duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
xdono | d10b8cf | 2008-07-02 12:55:45 -0700 | [diff] [blame] | 2 | * Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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 |
martin | 07f8b42 | 2009-09-08 14:33:59 -0700 | [diff] [blame] | 28 | * 6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958 |
sherman | 49c3dc4 | 2010-04-03 18:29:11 -0700 | [diff] [blame] | 29 | * 4947220 |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 30 | * @summary Basic tests for Process and Environment Variable code |
| 31 | * @run main/othervm Basic |
| 32 | * @author Martin Buchholz |
| 33 | */ |
| 34 | |
martin | dd2fd9f | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 35 | import java.lang.ProcessBuilder.Redirect; |
| 36 | import static java.lang.ProcessBuilder.Redirect.*; |
| 37 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 38 | import java.io.*; |
| 39 | import java.util.*; |
| 40 | import java.security.*; |
| 41 | import java.util.regex.Pattern; |
| 42 | import static java.lang.System.getenv; |
| 43 | import static java.lang.System.out; |
| 44 | import static java.lang.Boolean.TRUE; |
| 45 | import static java.util.AbstractMap.SimpleImmutableEntry; |
| 46 | |
| 47 | public class Basic { |
| 48 | |
| 49 | private static String commandOutput(Reader r) throws Throwable { |
| 50 | StringBuilder sb = new StringBuilder(); |
| 51 | int c; |
| 52 | while ((c = r.read()) > 0) |
| 53 | if (c != '\r') |
| 54 | sb.append((char) c); |
| 55 | return sb.toString(); |
| 56 | } |
| 57 | |
| 58 | private static String commandOutput(Process p) throws Throwable { |
| 59 | check(p.getInputStream() == p.getInputStream()); |
| 60 | check(p.getOutputStream() == p.getOutputStream()); |
| 61 | check(p.getErrorStream() == p.getErrorStream()); |
| 62 | Reader r = new InputStreamReader(p.getInputStream(),"UTF-8"); |
| 63 | String output = commandOutput(r); |
| 64 | equal(p.waitFor(), 0); |
| 65 | equal(p.exitValue(), 0); |
| 66 | return output; |
| 67 | } |
| 68 | |
| 69 | private static String commandOutput(ProcessBuilder pb) { |
| 70 | try { |
| 71 | return commandOutput(pb.start()); |
| 72 | } catch (Throwable t) { |
| 73 | String commandline = ""; |
| 74 | for (String arg : pb.command()) |
| 75 | commandline += " " + arg; |
| 76 | System.out.println("Exception trying to run process: " + commandline); |
| 77 | unexpected(t); |
| 78 | return ""; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | private static String commandOutput(String...command) { |
| 83 | try { |
| 84 | return commandOutput(Runtime.getRuntime().exec(command)); |
| 85 | } catch (Throwable t) { |
| 86 | String commandline = ""; |
| 87 | for (String arg : command) |
| 88 | commandline += " " + arg; |
| 89 | System.out.println("Exception trying to run process: " + commandline); |
| 90 | unexpected(t); |
| 91 | return ""; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private static void checkCommandOutput(ProcessBuilder pb, |
| 96 | String expected, |
| 97 | String failureMsg) { |
| 98 | String got = commandOutput(pb); |
| 99 | check(got.equals(expected), |
| 100 | failureMsg + "\n" + |
| 101 | "Expected: \"" + expected + "\"\n" + |
| 102 | "Got: \"" + got + "\""); |
| 103 | } |
| 104 | |
| 105 | private static String absolutifyPath(String path) { |
| 106 | StringBuilder sb = new StringBuilder(); |
| 107 | for (String file : path.split(File.pathSeparator)) { |
| 108 | if (sb.length() != 0) |
| 109 | sb.append(File.pathSeparator); |
| 110 | sb.append(new File(file).getAbsolutePath()); |
| 111 | } |
| 112 | return sb.toString(); |
| 113 | } |
| 114 | |
| 115 | // compare windows-style, by canonicalizing to upper case, |
| 116 | // not lower case as String.compareToIgnoreCase does |
| 117 | private static class WindowsComparator |
| 118 | implements Comparator<String> { |
| 119 | public int compare(String x, String y) { |
| 120 | return x.toUpperCase(Locale.US) |
| 121 | .compareTo(y.toUpperCase(Locale.US)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private static String sortedLines(String lines) { |
| 126 | String[] arr = lines.split("\n"); |
| 127 | List<String> ls = new ArrayList<String>(); |
| 128 | for (String s : arr) |
| 129 | ls.add(s); |
| 130 | Collections.sort(ls, new WindowsComparator()); |
| 131 | StringBuilder sb = new StringBuilder(); |
| 132 | for (String s : ls) |
| 133 | sb.append(s + "\n"); |
| 134 | return sb.toString(); |
| 135 | } |
| 136 | |
| 137 | private static void compareLinesIgnoreCase(String lines1, String lines2) { |
| 138 | if (! (sortedLines(lines1).equalsIgnoreCase(sortedLines(lines2)))) { |
| 139 | String dashes = |
| 140 | "-----------------------------------------------------"; |
| 141 | out.println(dashes); |
| 142 | out.print(sortedLines(lines1)); |
| 143 | out.println(dashes); |
| 144 | out.print(sortedLines(lines2)); |
| 145 | out.println(dashes); |
| 146 | out.println("sizes: " + sortedLines(lines1).length() + |
| 147 | " " + sortedLines(lines2).length()); |
| 148 | |
| 149 | fail("Sorted string contents differ"); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private static final Runtime runtime = Runtime.getRuntime(); |
| 154 | |
| 155 | private static final String[] winEnvCommand = {"cmd.exe", "/c", "set"}; |
| 156 | |
| 157 | private static String winEnvFilter(String env) { |
| 158 | return env.replaceAll("\r", "") |
| 159 | .replaceAll("(?m)^(?:COMSPEC|PROMPT|PATHEXT)=.*\n",""); |
| 160 | } |
| 161 | |
| 162 | private static String unixEnvProg() { |
| 163 | return new File("/usr/bin/env").canExecute() ? "/usr/bin/env" |
| 164 | : "/bin/env"; |
| 165 | } |
| 166 | |
| 167 | private static String nativeEnv(String[] env) { |
| 168 | try { |
| 169 | if (Windows.is()) { |
| 170 | return winEnvFilter |
| 171 | (commandOutput(runtime.exec(winEnvCommand, env))); |
| 172 | } else { |
| 173 | return commandOutput(runtime.exec(unixEnvProg(), env)); |
| 174 | } |
| 175 | } catch (Throwable t) { throw new Error(t); } |
| 176 | } |
| 177 | |
| 178 | private static String nativeEnv(ProcessBuilder pb) { |
| 179 | try { |
| 180 | if (Windows.is()) { |
| 181 | pb.command(winEnvCommand); |
| 182 | return winEnvFilter(commandOutput(pb)); |
| 183 | } else { |
| 184 | pb.command(new String[]{unixEnvProg()}); |
| 185 | return commandOutput(pb); |
| 186 | } |
| 187 | } catch (Throwable t) { throw new Error(t); } |
| 188 | } |
| 189 | |
| 190 | private static void checkSizes(Map<String,String> environ, int size) { |
| 191 | try { |
| 192 | equal(size, environ.size()); |
| 193 | equal(size, environ.entrySet().size()); |
| 194 | equal(size, environ.keySet().size()); |
| 195 | equal(size, environ.values().size()); |
| 196 | |
| 197 | boolean isEmpty = (size == 0); |
| 198 | equal(isEmpty, environ.isEmpty()); |
| 199 | equal(isEmpty, environ.entrySet().isEmpty()); |
| 200 | equal(isEmpty, environ.keySet().isEmpty()); |
| 201 | equal(isEmpty, environ.values().isEmpty()); |
| 202 | } catch (Throwable t) { unexpected(t); } |
| 203 | } |
| 204 | |
| 205 | private interface EnvironmentFrobber { |
| 206 | void doIt(Map<String,String> environ); |
| 207 | } |
| 208 | |
| 209 | private static void testVariableDeleter(EnvironmentFrobber fooDeleter) { |
| 210 | try { |
| 211 | Map<String,String> environ = new ProcessBuilder().environment(); |
| 212 | environ.put("Foo", "BAAR"); |
| 213 | fooDeleter.doIt(environ); |
| 214 | equal(environ.get("Foo"), null); |
| 215 | equal(environ.remove("Foo"), null); |
| 216 | } catch (Throwable t) { unexpected(t); } |
| 217 | } |
| 218 | |
| 219 | private static void testVariableAdder(EnvironmentFrobber fooAdder) { |
| 220 | try { |
| 221 | Map<String,String> environ = new ProcessBuilder().environment(); |
| 222 | environ.remove("Foo"); |
| 223 | fooAdder.doIt(environ); |
| 224 | equal(environ.get("Foo"), "Bahrein"); |
| 225 | } catch (Throwable t) { unexpected(t); } |
| 226 | } |
| 227 | |
| 228 | private static void testVariableModifier(EnvironmentFrobber fooModifier) { |
| 229 | try { |
| 230 | Map<String,String> environ = new ProcessBuilder().environment(); |
| 231 | environ.put("Foo","OldValue"); |
| 232 | fooModifier.doIt(environ); |
| 233 | equal(environ.get("Foo"), "NewValue"); |
| 234 | } catch (Throwable t) { unexpected(t); } |
| 235 | } |
| 236 | |
| 237 | private static void printUTF8(String s) throws IOException { |
| 238 | out.write(s.getBytes("UTF-8")); |
| 239 | } |
| 240 | |
| 241 | private static String getenvAsString(Map<String,String> environment) { |
| 242 | StringBuilder sb = new StringBuilder(); |
| 243 | for (Map.Entry<String,String> e : environment.entrySet()) |
| 244 | // Ignore magic environment variables added by the launcher |
| 245 | if (! e.getKey().equals("NLSPATH") && |
| 246 | ! e.getKey().equals("XFILESEARCHPATH") && |
| 247 | ! e.getKey().equals("LD_LIBRARY_PATH")) |
| 248 | sb.append(e.getKey()) |
| 249 | .append('=') |
| 250 | .append(e.getValue()) |
| 251 | .append(','); |
| 252 | return sb.toString(); |
| 253 | } |
| 254 | |
| 255 | static void print4095(OutputStream s) throws Throwable { |
| 256 | byte[] bytes = new byte[4095]; |
| 257 | Arrays.fill(bytes, (byte) '!'); |
| 258 | s.write(bytes); // Might hang! |
| 259 | } |
| 260 | |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 261 | static void checkPermissionDenied(ProcessBuilder pb) { |
| 262 | try { |
| 263 | pb.start(); |
| 264 | fail("Expected IOException not thrown"); |
| 265 | } catch (IOException e) { |
| 266 | String m = e.getMessage(); |
| 267 | if (EnglishUnix.is() && |
| 268 | ! matches(m, "Permission denied")) |
| 269 | unexpected(e); |
| 270 | } catch (Throwable t) { unexpected(t); } |
| 271 | } |
| 272 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 273 | public static class JavaChild { |
| 274 | public static void main(String args[]) throws Throwable { |
| 275 | String action = args[0]; |
martin | dd2fd9f | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 276 | if (action.equals("testIO")) { |
| 277 | String expected = "standard input"; |
| 278 | char[] buf = new char[expected.length()+1]; |
| 279 | int n = new InputStreamReader(System.in).read(buf,0,buf.length); |
| 280 | if (n != expected.length()) |
| 281 | System.exit(5); |
| 282 | if (! new String(buf,0,n).equals(expected)) |
| 283 | System.exit(5); |
| 284 | System.err.print("standard error"); |
| 285 | System.out.print("standard output"); |
| 286 | } else if (action.equals("testInheritIO")) { |
| 287 | List<String> childArgs = new ArrayList<String>(javaChildArgs); |
| 288 | childArgs.add("testIO"); |
| 289 | ProcessBuilder pb = new ProcessBuilder(childArgs); |
| 290 | pb.inheritIO(); |
| 291 | ProcessResults r = run(pb); |
| 292 | if (! r.out().equals("")) |
| 293 | System.exit(7); |
| 294 | if (! r.err().equals("")) |
| 295 | System.exit(8); |
| 296 | if (r.exitValue() != 0) |
| 297 | System.exit(9); |
| 298 | } else if (action.equals("System.getenv(String)")) { |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 299 | String val = System.getenv(args[1]); |
| 300 | printUTF8(val == null ? "null" : val); |
| 301 | } else if (action.equals("System.getenv(\\u1234)")) { |
| 302 | String val = System.getenv("\u1234"); |
| 303 | printUTF8(val == null ? "null" : val); |
| 304 | } else if (action.equals("System.getenv()")) { |
| 305 | printUTF8(getenvAsString(System.getenv())); |
martin | 07f8b42 | 2009-09-08 14:33:59 -0700 | [diff] [blame] | 306 | } else if (action.equals("ArrayOOME")) { |
| 307 | Object dummy; |
| 308 | switch(new Random().nextInt(3)) { |
| 309 | case 0: dummy = new Integer[Integer.MAX_VALUE]; break; |
| 310 | case 1: dummy = new double[Integer.MAX_VALUE]; break; |
| 311 | case 2: dummy = new byte[Integer.MAX_VALUE][]; break; |
| 312 | default: throw new InternalError(); |
| 313 | } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 314 | } else if (action.equals("pwd")) { |
| 315 | printUTF8(new File(System.getProperty("user.dir")) |
| 316 | .getCanonicalPath()); |
| 317 | } else if (action.equals("print4095")) { |
| 318 | print4095(System.out); |
| 319 | System.exit(5); |
| 320 | } else if (action.equals("OutErr")) { |
| 321 | // You might think the system streams would be |
| 322 | // buffered, and in fact they are implemented using |
| 323 | // BufferedOutputStream, but each and every print |
| 324 | // causes immediate operating system I/O. |
| 325 | System.out.print("out"); |
| 326 | System.err.print("err"); |
| 327 | System.out.print("out"); |
| 328 | System.err.print("err"); |
| 329 | } else if (action.equals("null PATH")) { |
| 330 | equal(System.getenv("PATH"), null); |
| 331 | check(new File("/bin/true").exists()); |
| 332 | check(new File("/bin/false").exists()); |
| 333 | ProcessBuilder pb1 = new ProcessBuilder(); |
| 334 | ProcessBuilder pb2 = new ProcessBuilder(); |
| 335 | pb2.environment().put("PATH", "anyOldPathIgnoredAnyways"); |
| 336 | ProcessResults r; |
| 337 | |
| 338 | for (final ProcessBuilder pb : |
| 339 | new ProcessBuilder[] {pb1, pb2}) { |
| 340 | pb.command("true"); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 341 | equal(run(pb).exitValue(), True.exitValue()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 342 | |
| 343 | pb.command("false"); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 344 | equal(run(pb).exitValue(), False.exitValue()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (failed != 0) throw new Error("null PATH"); |
| 348 | } else if (action.equals("PATH search algorithm")) { |
| 349 | equal(System.getenv("PATH"), "dir1:dir2:"); |
| 350 | check(new File("/bin/true").exists()); |
| 351 | check(new File("/bin/false").exists()); |
| 352 | String[] cmd = {"prog"}; |
| 353 | ProcessBuilder pb1 = new ProcessBuilder(cmd); |
| 354 | ProcessBuilder pb2 = new ProcessBuilder(cmd); |
| 355 | ProcessBuilder pb3 = new ProcessBuilder(cmd); |
| 356 | pb2.environment().put("PATH", "anyOldPathIgnoredAnyways"); |
| 357 | pb3.environment().remove("PATH"); |
| 358 | |
| 359 | for (final ProcessBuilder pb : |
| 360 | new ProcessBuilder[] {pb1, pb2, pb3}) { |
| 361 | try { |
| 362 | // Not on PATH at all; directories don't exist |
| 363 | try { |
| 364 | pb.start(); |
| 365 | fail("Expected IOException not thrown"); |
| 366 | } catch (IOException e) { |
| 367 | String m = e.getMessage(); |
| 368 | if (EnglishUnix.is() && |
| 369 | ! matches(m, "No such file")) |
| 370 | unexpected(e); |
| 371 | } catch (Throwable t) { unexpected(t); } |
| 372 | |
| 373 | // Not on PATH at all; directories exist |
| 374 | new File("dir1").mkdirs(); |
| 375 | new File("dir2").mkdirs(); |
| 376 | try { |
| 377 | pb.start(); |
| 378 | fail("Expected IOException not thrown"); |
| 379 | } catch (IOException e) { |
| 380 | String m = e.getMessage(); |
| 381 | if (EnglishUnix.is() && |
| 382 | ! matches(m, "No such file")) |
| 383 | unexpected(e); |
| 384 | } catch (Throwable t) { unexpected(t); } |
| 385 | |
| 386 | // Can't execute a directory -- permission denied |
| 387 | // Report EACCES errno |
| 388 | new File("dir1/prog").mkdirs(); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 389 | checkPermissionDenied(pb); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 390 | |
| 391 | // continue searching if EACCES |
| 392 | copy("/bin/true", "dir2/prog"); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 393 | equal(run(pb).exitValue(), True.exitValue()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 394 | new File("dir1/prog").delete(); |
| 395 | new File("dir2/prog").delete(); |
| 396 | |
| 397 | new File("dir2/prog").mkdirs(); |
| 398 | copy("/bin/true", "dir1/prog"); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 399 | equal(run(pb).exitValue(), True.exitValue()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 400 | |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 401 | // Check empty PATH component means current directory. |
| 402 | // |
| 403 | // While we're here, let's test different kinds of |
| 404 | // Unix executables, and PATH vs explicit searching. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 405 | new File("dir1/prog").delete(); |
| 406 | new File("dir2/prog").delete(); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 407 | for (String[] command : |
| 408 | new String[][] { |
| 409 | new String[] {"./prog"}, |
| 410 | cmd}) { |
| 411 | pb.command(command); |
| 412 | File prog = new File("./prog"); |
| 413 | // "Normal" binaries |
| 414 | copy("/bin/true", "./prog"); |
| 415 | equal(run(pb).exitValue(), |
| 416 | True.exitValue()); |
| 417 | copy("/bin/false", "./prog"); |
| 418 | equal(run(pb).exitValue(), |
| 419 | False.exitValue()); |
| 420 | prog.delete(); |
| 421 | // Interpreter scripts with #! |
| 422 | setFileContents(prog, "#!/bin/true\n"); |
| 423 | prog.setExecutable(true); |
| 424 | equal(run(pb).exitValue(), |
| 425 | True.exitValue()); |
| 426 | prog.delete(); |
| 427 | setFileContents(prog, "#!/bin/false\n"); |
| 428 | prog.setExecutable(true); |
| 429 | equal(run(pb).exitValue(), |
| 430 | False.exitValue()); |
| 431 | // Traditional shell scripts without #! |
| 432 | setFileContents(prog, "exec /bin/true\n"); |
| 433 | prog.setExecutable(true); |
| 434 | equal(run(pb).exitValue(), |
| 435 | True.exitValue()); |
| 436 | prog.delete(); |
| 437 | setFileContents(prog, "exec /bin/false\n"); |
| 438 | prog.setExecutable(true); |
| 439 | equal(run(pb).exitValue(), |
| 440 | False.exitValue()); |
| 441 | prog.delete(); |
| 442 | } |
| 443 | |
| 444 | // Test Unix interpreter scripts |
| 445 | File dir1Prog = new File("dir1/prog"); |
| 446 | dir1Prog.delete(); |
| 447 | pb.command(new String[] {"prog", "world"}); |
| 448 | setFileContents(dir1Prog, "#!/bin/echo hello\n"); |
| 449 | checkPermissionDenied(pb); |
| 450 | dir1Prog.setExecutable(true); |
| 451 | equal(run(pb).out(), "hello dir1/prog world\n"); |
| 452 | equal(run(pb).exitValue(), True.exitValue()); |
| 453 | dir1Prog.delete(); |
| 454 | pb.command(cmd); |
| 455 | |
| 456 | // Test traditional shell scripts without #! |
| 457 | setFileContents(dir1Prog, "/bin/echo \"$@\"\n"); |
| 458 | pb.command(new String[] {"prog", "hello", "world"}); |
| 459 | checkPermissionDenied(pb); |
| 460 | dir1Prog.setExecutable(true); |
| 461 | equal(run(pb).out(), "hello world\n"); |
| 462 | equal(run(pb).exitValue(), True.exitValue()); |
| 463 | dir1Prog.delete(); |
| 464 | pb.command(cmd); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 465 | |
| 466 | // If prog found on both parent and child's PATH, |
| 467 | // parent's is used. |
| 468 | new File("dir1/prog").delete(); |
| 469 | new File("dir2/prog").delete(); |
| 470 | new File("prog").delete(); |
| 471 | new File("dir3").mkdirs(); |
| 472 | copy("/bin/true", "dir1/prog"); |
| 473 | copy("/bin/false", "dir3/prog"); |
| 474 | pb.environment().put("PATH","dir3"); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 475 | equal(run(pb).exitValue(), True.exitValue()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 476 | copy("/bin/true", "dir3/prog"); |
| 477 | copy("/bin/false", "dir1/prog"); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 478 | equal(run(pb).exitValue(), False.exitValue()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 479 | |
| 480 | } finally { |
| 481 | // cleanup |
| 482 | new File("dir1/prog").delete(); |
| 483 | new File("dir2/prog").delete(); |
| 484 | new File("dir3/prog").delete(); |
| 485 | new File("dir1").delete(); |
| 486 | new File("dir2").delete(); |
| 487 | new File("dir3").delete(); |
| 488 | new File("prog").delete(); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | if (failed != 0) throw new Error("PATH search algorithm"); |
| 493 | } |
| 494 | else throw new Error("JavaChild invocation error"); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | private static void copy(String src, String dst) { |
| 499 | system("/bin/cp", "-fp", src, dst); |
| 500 | } |
| 501 | |
| 502 | private static void system(String... command) { |
| 503 | try { |
| 504 | ProcessBuilder pb = new ProcessBuilder(command); |
| 505 | ProcessResults r = run(pb.start()); |
| 506 | equal(r.exitValue(), 0); |
| 507 | equal(r.out(), ""); |
| 508 | equal(r.err(), ""); |
| 509 | } catch (Throwable t) { unexpected(t); } |
| 510 | } |
| 511 | |
| 512 | private static String javaChildOutput(ProcessBuilder pb, String...args) { |
| 513 | List<String> list = new ArrayList<String>(javaChildArgs); |
| 514 | for (String arg : args) |
| 515 | list.add(arg); |
| 516 | pb.command(list); |
| 517 | return commandOutput(pb); |
| 518 | } |
| 519 | |
| 520 | private static String getenvInChild(ProcessBuilder pb) { |
| 521 | return javaChildOutput(pb, "System.getenv()"); |
| 522 | } |
| 523 | |
| 524 | private static String getenvInChild1234(ProcessBuilder pb) { |
| 525 | return javaChildOutput(pb, "System.getenv(\\u1234)"); |
| 526 | } |
| 527 | |
| 528 | private static String getenvInChild(ProcessBuilder pb, String name) { |
| 529 | return javaChildOutput(pb, "System.getenv(String)", name); |
| 530 | } |
| 531 | |
| 532 | private static String pwdInChild(ProcessBuilder pb) { |
| 533 | return javaChildOutput(pb, "pwd"); |
| 534 | } |
| 535 | |
| 536 | private static final String javaExe = |
| 537 | System.getProperty("java.home") + |
| 538 | File.separator + "bin" + File.separator + "java"; |
| 539 | |
| 540 | private static final String classpath = |
| 541 | System.getProperty("java.class.path"); |
| 542 | |
| 543 | private static final List<String> javaChildArgs = |
| 544 | Arrays.asList(new String[] |
| 545 | { javaExe, "-classpath", absolutifyPath(classpath), |
| 546 | "Basic$JavaChild"}); |
| 547 | |
| 548 | private static void testEncoding(String encoding, String tested) { |
| 549 | try { |
| 550 | // If round trip conversion works, should be able to set env vars |
| 551 | // correctly in child. |
| 552 | if (new String(tested.getBytes()).equals(tested)) { |
| 553 | out.println("Testing " + encoding + " environment values"); |
| 554 | ProcessBuilder pb = new ProcessBuilder(); |
| 555 | pb.environment().put("ASCIINAME",tested); |
| 556 | equal(getenvInChild(pb,"ASCIINAME"), tested); |
| 557 | } |
| 558 | } catch (Throwable t) { unexpected(t); } |
| 559 | } |
| 560 | |
| 561 | static class Windows { |
| 562 | public static boolean is() { return is; } |
| 563 | private static final boolean is = |
| 564 | System.getProperty("os.name").startsWith("Windows"); |
| 565 | } |
| 566 | |
| 567 | static class Unix { |
| 568 | public static boolean is() { return is; } |
| 569 | private static final boolean is = |
| 570 | (! Windows.is() && |
| 571 | new File("/bin/sh").exists() && |
| 572 | new File("/bin/true").exists() && |
| 573 | new File("/bin/false").exists()); |
| 574 | } |
| 575 | |
| 576 | static class UnicodeOS { |
| 577 | public static boolean is() { return is; } |
| 578 | private static final String osName = System.getProperty("os.name"); |
| 579 | private static final boolean is = |
| 580 | // MacOS X would probably also qualify |
| 581 | osName.startsWith("Windows") && |
| 582 | ! osName.startsWith("Windows 9") && |
| 583 | ! osName.equals("Windows Me"); |
| 584 | } |
| 585 | |
| 586 | static class True { |
| 587 | public static int exitValue() { return 0; } |
| 588 | } |
| 589 | |
| 590 | private static class False { |
| 591 | public static int exitValue() { return exitValue; } |
| 592 | private static final int exitValue = exitValue0(); |
| 593 | private static int exitValue0() { |
| 594 | // /bin/false returns an *unspecified* non-zero number. |
| 595 | try { |
| 596 | if (! Unix.is()) |
| 597 | return -1; |
| 598 | else { |
| 599 | int rc = new ProcessBuilder("/bin/false") |
| 600 | .start().waitFor(); |
| 601 | check(rc != 0); |
| 602 | return rc; |
| 603 | } |
| 604 | } catch (Throwable t) { unexpected(t); return -1; } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | static class EnglishUnix { |
| 609 | private final static Boolean is = |
| 610 | (! Windows.is() && isEnglish("LANG") && isEnglish("LC_ALL")); |
| 611 | |
| 612 | private static boolean isEnglish(String envvar) { |
| 613 | String val = getenv(envvar); |
| 614 | return (val == null) || val.matches("en.*"); |
| 615 | } |
| 616 | |
| 617 | /** Returns true if we can expect English OS error strings */ |
| 618 | static boolean is() { return is; } |
| 619 | } |
| 620 | |
| 621 | private static boolean matches(String str, String regex) { |
| 622 | return Pattern.compile(regex).matcher(str).find(); |
| 623 | } |
| 624 | |
| 625 | private static String sortByLinesWindowsly(String text) { |
| 626 | String[] lines = text.split("\n"); |
| 627 | Arrays.sort(lines, new WindowsComparator()); |
| 628 | StringBuilder sb = new StringBuilder(); |
| 629 | for (String line : lines) |
| 630 | sb.append(line).append("\n"); |
| 631 | return sb.toString(); |
| 632 | } |
| 633 | |
| 634 | private static void checkMapSanity(Map<String,String> map) { |
| 635 | try { |
| 636 | Set<String> keySet = map.keySet(); |
| 637 | Collection<String> values = map.values(); |
| 638 | Set<Map.Entry<String,String>> entrySet = map.entrySet(); |
| 639 | |
| 640 | equal(entrySet.size(), keySet.size()); |
| 641 | equal(entrySet.size(), values.size()); |
| 642 | |
| 643 | StringBuilder s1 = new StringBuilder(); |
| 644 | for (Map.Entry<String,String> e : entrySet) |
| 645 | s1.append(e.getKey() + "=" + e.getValue() + "\n"); |
| 646 | |
| 647 | StringBuilder s2 = new StringBuilder(); |
| 648 | for (String var : keySet) |
| 649 | s2.append(var + "=" + map.get(var) + "\n"); |
| 650 | |
| 651 | equal(s1.toString(), s2.toString()); |
| 652 | |
| 653 | Iterator<String> kIter = keySet.iterator(); |
| 654 | Iterator<String> vIter = values.iterator(); |
| 655 | Iterator<Map.Entry<String,String>> eIter = entrySet.iterator(); |
| 656 | |
| 657 | while (eIter.hasNext()) { |
| 658 | Map.Entry<String,String> entry = eIter.next(); |
| 659 | String key = kIter.next(); |
| 660 | String value = vIter.next(); |
| 661 | check(entrySet.contains(entry)); |
| 662 | check(keySet.contains(key)); |
| 663 | check(values.contains(value)); |
| 664 | check(map.containsKey(key)); |
| 665 | check(map.containsValue(value)); |
| 666 | equal(entry.getKey(), key); |
| 667 | equal(entry.getValue(), value); |
| 668 | } |
| 669 | check(! kIter.hasNext() && |
| 670 | ! vIter.hasNext()); |
| 671 | |
| 672 | } catch (Throwable t) { unexpected(t); } |
| 673 | } |
| 674 | |
| 675 | private static void checkMapEquality(Map<String,String> map1, |
| 676 | Map<String,String> map2) { |
| 677 | try { |
| 678 | equal(map1.size(), map2.size()); |
| 679 | equal(map1.isEmpty(), map2.isEmpty()); |
| 680 | for (String key : map1.keySet()) { |
| 681 | equal(map1.get(key), map2.get(key)); |
| 682 | check(map2.keySet().contains(key)); |
| 683 | } |
| 684 | equal(map1, map2); |
| 685 | equal(map2, map1); |
| 686 | equal(map1.entrySet(), map2.entrySet()); |
| 687 | equal(map2.entrySet(), map1.entrySet()); |
| 688 | equal(map1.keySet(), map2.keySet()); |
| 689 | equal(map2.keySet(), map1.keySet()); |
| 690 | |
| 691 | equal(map1.hashCode(), map2.hashCode()); |
| 692 | equal(map1.entrySet().hashCode(), map2.entrySet().hashCode()); |
| 693 | equal(map1.keySet().hashCode(), map2.keySet().hashCode()); |
| 694 | } catch (Throwable t) { unexpected(t); } |
| 695 | } |
| 696 | |
martin | dd2fd9f | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 697 | static void checkRedirects(ProcessBuilder pb, |
| 698 | Redirect in, Redirect out, Redirect err) { |
| 699 | equal(pb.redirectInput(), in); |
| 700 | equal(pb.redirectOutput(), out); |
| 701 | equal(pb.redirectError(), err); |
| 702 | } |
| 703 | |
| 704 | static void redirectIO(ProcessBuilder pb, |
| 705 | Redirect in, Redirect out, Redirect err) { |
| 706 | pb.redirectInput(in); |
| 707 | pb.redirectOutput(out); |
| 708 | pb.redirectError(err); |
| 709 | } |
| 710 | |
| 711 | static void setFileContents(File file, String contents) { |
| 712 | try { |
| 713 | Writer w = new FileWriter(file); |
| 714 | w.write(contents); |
| 715 | w.close(); |
| 716 | } catch (Throwable t) { unexpected(t); } |
| 717 | } |
| 718 | |
| 719 | static String fileContents(File file) { |
| 720 | try { |
| 721 | Reader r = new FileReader(file); |
| 722 | StringBuilder sb = new StringBuilder(); |
| 723 | char[] buffer = new char[1024]; |
| 724 | int n; |
| 725 | while ((n = r.read(buffer)) != -1) |
| 726 | sb.append(buffer,0,n); |
| 727 | r.close(); |
| 728 | return new String(sb); |
| 729 | } catch (Throwable t) { unexpected(t); return ""; } |
| 730 | } |
| 731 | |
| 732 | static void testIORedirection() throws Throwable { |
| 733 | final File ifile = new File("ifile"); |
| 734 | final File ofile = new File("ofile"); |
| 735 | final File efile = new File("efile"); |
| 736 | ifile.delete(); |
| 737 | ofile.delete(); |
| 738 | efile.delete(); |
| 739 | |
| 740 | //---------------------------------------------------------------- |
| 741 | // Check mutual inequality of different types of Redirect |
| 742 | //---------------------------------------------------------------- |
| 743 | Redirect[] redirects = |
| 744 | { PIPE, |
| 745 | INHERIT, |
| 746 | Redirect.from(ifile), |
| 747 | Redirect.to(ifile), |
| 748 | Redirect.appendTo(ifile), |
| 749 | Redirect.from(ofile), |
| 750 | Redirect.to(ofile), |
| 751 | Redirect.appendTo(ofile), |
| 752 | }; |
| 753 | for (int i = 0; i < redirects.length; i++) |
| 754 | for (int j = 0; j < redirects.length; j++) |
| 755 | equal(redirects[i].equals(redirects[j]), (i == j)); |
| 756 | |
| 757 | //---------------------------------------------------------------- |
| 758 | // Check basic properties of different types of Redirect |
| 759 | //---------------------------------------------------------------- |
| 760 | equal(PIPE.type(), Redirect.Type.PIPE); |
| 761 | equal(PIPE.toString(), "PIPE"); |
| 762 | equal(PIPE.file(), null); |
| 763 | |
| 764 | equal(INHERIT.type(), Redirect.Type.INHERIT); |
| 765 | equal(INHERIT.toString(), "INHERIT"); |
| 766 | equal(INHERIT.file(), null); |
| 767 | |
| 768 | equal(Redirect.from(ifile).type(), Redirect.Type.READ); |
| 769 | equal(Redirect.from(ifile).toString(), |
| 770 | "redirect to read from file \"ifile\""); |
| 771 | equal(Redirect.from(ifile).file(), ifile); |
| 772 | equal(Redirect.from(ifile), |
| 773 | Redirect.from(ifile)); |
| 774 | equal(Redirect.from(ifile).hashCode(), |
| 775 | Redirect.from(ifile).hashCode()); |
| 776 | |
| 777 | equal(Redirect.to(ofile).type(), Redirect.Type.WRITE); |
| 778 | equal(Redirect.to(ofile).toString(), |
| 779 | "redirect to write to file \"ofile\""); |
| 780 | equal(Redirect.to(ofile).file(), ofile); |
| 781 | equal(Redirect.to(ofile), |
| 782 | Redirect.to(ofile)); |
| 783 | equal(Redirect.to(ofile).hashCode(), |
| 784 | Redirect.to(ofile).hashCode()); |
| 785 | |
| 786 | equal(Redirect.appendTo(ofile).type(), Redirect.Type.APPEND); |
| 787 | equal(Redirect.appendTo(efile).toString(), |
| 788 | "redirect to append to file \"efile\""); |
| 789 | equal(Redirect.appendTo(efile).file(), efile); |
| 790 | equal(Redirect.appendTo(efile), |
| 791 | Redirect.appendTo(efile)); |
| 792 | equal(Redirect.appendTo(efile).hashCode(), |
| 793 | Redirect.appendTo(efile).hashCode()); |
| 794 | |
| 795 | //---------------------------------------------------------------- |
| 796 | // Check initial values of redirects |
| 797 | //---------------------------------------------------------------- |
| 798 | List<String> childArgs = new ArrayList<String>(javaChildArgs); |
| 799 | childArgs.add("testIO"); |
| 800 | final ProcessBuilder pb = new ProcessBuilder(childArgs); |
| 801 | checkRedirects(pb, PIPE, PIPE, PIPE); |
| 802 | |
| 803 | //---------------------------------------------------------------- |
| 804 | // Check inheritIO |
| 805 | //---------------------------------------------------------------- |
| 806 | pb.inheritIO(); |
| 807 | checkRedirects(pb, INHERIT, INHERIT, INHERIT); |
| 808 | |
| 809 | //---------------------------------------------------------------- |
| 810 | // Check setters and getters agree |
| 811 | //---------------------------------------------------------------- |
| 812 | pb.redirectInput(ifile); |
| 813 | equal(pb.redirectInput().file(), ifile); |
| 814 | equal(pb.redirectInput(), Redirect.from(ifile)); |
| 815 | |
| 816 | pb.redirectOutput(ofile); |
| 817 | equal(pb.redirectOutput().file(), ofile); |
| 818 | equal(pb.redirectOutput(), Redirect.to(ofile)); |
| 819 | |
| 820 | pb.redirectError(efile); |
| 821 | equal(pb.redirectError().file(), efile); |
| 822 | equal(pb.redirectError(), Redirect.to(efile)); |
| 823 | |
| 824 | THROWS(IllegalArgumentException.class, |
| 825 | new Fun(){void f() { |
| 826 | pb.redirectInput(Redirect.to(ofile)); }}, |
| 827 | new Fun(){void f() { |
| 828 | pb.redirectInput(Redirect.appendTo(ofile)); }}, |
| 829 | new Fun(){void f() { |
| 830 | pb.redirectOutput(Redirect.from(ifile)); }}, |
| 831 | new Fun(){void f() { |
| 832 | pb.redirectError(Redirect.from(ifile)); }}); |
| 833 | |
| 834 | THROWS(IOException.class, |
| 835 | // Input file does not exist |
| 836 | new Fun(){void f() throws Throwable { pb.start(); }}); |
| 837 | setFileContents(ifile, "standard input"); |
| 838 | |
| 839 | //---------------------------------------------------------------- |
| 840 | // Writing to non-existent files |
| 841 | //---------------------------------------------------------------- |
| 842 | { |
| 843 | ProcessResults r = run(pb); |
| 844 | equal(r.exitValue(), 0); |
| 845 | equal(fileContents(ofile), "standard output"); |
| 846 | equal(fileContents(efile), "standard error"); |
| 847 | equal(r.out(), ""); |
| 848 | equal(r.err(), ""); |
| 849 | ofile.delete(); |
| 850 | efile.delete(); |
| 851 | } |
| 852 | |
| 853 | //---------------------------------------------------------------- |
| 854 | // Both redirectErrorStream + redirectError |
| 855 | //---------------------------------------------------------------- |
| 856 | { |
| 857 | pb.redirectErrorStream(true); |
| 858 | ProcessResults r = run(pb); |
| 859 | equal(r.exitValue(), 0); |
| 860 | equal(fileContents(ofile), |
| 861 | "standard error" + "standard output"); |
| 862 | equal(fileContents(efile), ""); |
| 863 | equal(r.out(), ""); |
| 864 | equal(r.err(), ""); |
| 865 | ofile.delete(); |
| 866 | efile.delete(); |
| 867 | } |
| 868 | |
| 869 | //---------------------------------------------------------------- |
| 870 | // Appending to existing files |
| 871 | //---------------------------------------------------------------- |
| 872 | { |
| 873 | setFileContents(ofile, "ofile-contents"); |
| 874 | setFileContents(efile, "efile-contents"); |
| 875 | pb.redirectOutput(Redirect.appendTo(ofile)); |
| 876 | pb.redirectError(Redirect.appendTo(efile)); |
| 877 | pb.redirectErrorStream(false); |
| 878 | ProcessResults r = run(pb); |
| 879 | equal(r.exitValue(), 0); |
| 880 | equal(fileContents(ofile), |
| 881 | "ofile-contents" + "standard output"); |
| 882 | equal(fileContents(efile), |
| 883 | "efile-contents" + "standard error"); |
| 884 | equal(r.out(), ""); |
| 885 | equal(r.err(), ""); |
| 886 | ofile.delete(); |
| 887 | efile.delete(); |
| 888 | } |
| 889 | |
| 890 | //---------------------------------------------------------------- |
| 891 | // Replacing existing files |
| 892 | //---------------------------------------------------------------- |
| 893 | { |
| 894 | setFileContents(ofile, "ofile-contents"); |
| 895 | setFileContents(efile, "efile-contents"); |
| 896 | pb.redirectOutput(ofile); |
| 897 | pb.redirectError(Redirect.to(efile)); |
| 898 | ProcessResults r = run(pb); |
| 899 | equal(r.exitValue(), 0); |
| 900 | equal(fileContents(ofile), "standard output"); |
| 901 | equal(fileContents(efile), "standard error"); |
| 902 | equal(r.out(), ""); |
| 903 | equal(r.err(), ""); |
| 904 | ofile.delete(); |
| 905 | efile.delete(); |
| 906 | } |
| 907 | |
| 908 | //---------------------------------------------------------------- |
| 909 | // Appending twice to the same file? |
| 910 | //---------------------------------------------------------------- |
| 911 | { |
| 912 | setFileContents(ofile, "ofile-contents"); |
| 913 | setFileContents(efile, "efile-contents"); |
| 914 | Redirect appender = Redirect.appendTo(ofile); |
| 915 | pb.redirectOutput(appender); |
| 916 | pb.redirectError(appender); |
| 917 | ProcessResults r = run(pb); |
| 918 | equal(r.exitValue(), 0); |
| 919 | equal(fileContents(ofile), |
| 920 | "ofile-contents" + |
| 921 | "standard error" + |
| 922 | "standard output"); |
| 923 | equal(fileContents(efile), "efile-contents"); |
| 924 | equal(r.out(), ""); |
| 925 | equal(r.err(), ""); |
| 926 | ifile.delete(); |
| 927 | ofile.delete(); |
| 928 | efile.delete(); |
| 929 | } |
| 930 | |
| 931 | //---------------------------------------------------------------- |
| 932 | // Testing INHERIT is harder. |
| 933 | // Note that this requires __FOUR__ nested JVMs involved in one test, |
| 934 | // if you count the harness JVM. |
| 935 | //---------------------------------------------------------------- |
| 936 | { |
| 937 | redirectIO(pb, PIPE, PIPE, PIPE); |
| 938 | List<String> command = pb.command(); |
| 939 | command.set(command.size() - 1, "testInheritIO"); |
| 940 | Process p = pb.start(); |
| 941 | new PrintStream(p.getOutputStream()).print("standard input"); |
| 942 | p.getOutputStream().close(); |
| 943 | ProcessResults r = run(p); |
| 944 | equal(r.exitValue(), 0); |
| 945 | equal(r.out(), "standard output"); |
| 946 | equal(r.err(), "standard error"); |
| 947 | } |
| 948 | |
| 949 | //---------------------------------------------------------------- |
| 950 | // Test security implications of I/O redirection |
| 951 | //---------------------------------------------------------------- |
| 952 | |
| 953 | // Read access to current directory is always granted; |
| 954 | // So create a tmpfile for input instead. |
| 955 | final File tmpFile = File.createTempFile("Basic", "tmp"); |
| 956 | setFileContents(tmpFile, "standard input"); |
| 957 | |
| 958 | final Policy policy = new Policy(); |
| 959 | Policy.setPolicy(policy); |
| 960 | System.setSecurityManager(new SecurityManager()); |
| 961 | try { |
| 962 | final Permission xPermission |
| 963 | = new FilePermission("<<ALL FILES>>", "execute"); |
| 964 | final Permission rxPermission |
| 965 | = new FilePermission("<<ALL FILES>>", "read,execute"); |
| 966 | final Permission wxPermission |
| 967 | = new FilePermission("<<ALL FILES>>", "write,execute"); |
| 968 | final Permission rwxPermission |
| 969 | = new FilePermission("<<ALL FILES>>", "read,write,execute"); |
| 970 | |
| 971 | THROWS(SecurityException.class, |
| 972 | new Fun() { void f() throws IOException { |
| 973 | policy.setPermissions(xPermission); |
| 974 | redirectIO(pb, from(tmpFile), PIPE, PIPE); |
| 975 | pb.start();}}, |
| 976 | new Fun() { void f() throws IOException { |
| 977 | policy.setPermissions(rxPermission); |
| 978 | redirectIO(pb, PIPE, to(ofile), PIPE); |
| 979 | pb.start();}}, |
| 980 | new Fun() { void f() throws IOException { |
| 981 | policy.setPermissions(rxPermission); |
| 982 | redirectIO(pb, PIPE, PIPE, to(efile)); |
| 983 | pb.start();}}); |
| 984 | |
| 985 | { |
| 986 | policy.setPermissions(rxPermission); |
| 987 | redirectIO(pb, from(tmpFile), PIPE, PIPE); |
| 988 | ProcessResults r = run(pb); |
| 989 | equal(r.out(), "standard output"); |
| 990 | equal(r.err(), "standard error"); |
| 991 | } |
| 992 | |
| 993 | { |
| 994 | policy.setPermissions(wxPermission); |
| 995 | redirectIO(pb, PIPE, to(ofile), to(efile)); |
| 996 | Process p = pb.start(); |
| 997 | new PrintStream(p.getOutputStream()).print("standard input"); |
| 998 | p.getOutputStream().close(); |
| 999 | ProcessResults r = run(p); |
| 1000 | policy.setPermissions(rwxPermission); |
| 1001 | equal(fileContents(ofile), "standard output"); |
| 1002 | equal(fileContents(efile), "standard error"); |
| 1003 | } |
| 1004 | |
| 1005 | { |
| 1006 | policy.setPermissions(rwxPermission); |
| 1007 | redirectIO(pb, from(tmpFile), to(ofile), to(efile)); |
| 1008 | ProcessResults r = run(pb); |
| 1009 | policy.setPermissions(rwxPermission); |
| 1010 | equal(fileContents(ofile), "standard output"); |
| 1011 | equal(fileContents(efile), "standard error"); |
| 1012 | } |
| 1013 | |
| 1014 | } finally { |
| 1015 | policy.setPermissions(new RuntimePermission("setSecurityManager")); |
| 1016 | System.setSecurityManager(null); |
| 1017 | tmpFile.delete(); |
| 1018 | ifile.delete(); |
| 1019 | ofile.delete(); |
| 1020 | efile.delete(); |
| 1021 | } |
| 1022 | } |
| 1023 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1024 | private static void realMain(String[] args) throws Throwable { |
| 1025 | if (Windows.is()) |
| 1026 | System.out.println("This appears to be a Windows system."); |
| 1027 | if (Unix.is()) |
| 1028 | System.out.println("This appears to be a Unix system."); |
| 1029 | if (UnicodeOS.is()) |
| 1030 | System.out.println("This appears to be a Unicode-based OS."); |
| 1031 | |
martin | dd2fd9f | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 1032 | try { testIORedirection(); } |
| 1033 | catch (Throwable t) { unexpected(t); } |
| 1034 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1035 | //---------------------------------------------------------------- |
| 1036 | // Basic tests for setting, replacing and deleting envvars |
| 1037 | //---------------------------------------------------------------- |
| 1038 | try { |
| 1039 | ProcessBuilder pb = new ProcessBuilder(); |
| 1040 | Map<String,String> environ = pb.environment(); |
| 1041 | |
| 1042 | // New env var |
| 1043 | environ.put("QUUX", "BAR"); |
| 1044 | equal(environ.get("QUUX"), "BAR"); |
| 1045 | equal(getenvInChild(pb,"QUUX"), "BAR"); |
| 1046 | |
| 1047 | // Modify env var |
| 1048 | environ.put("QUUX","bear"); |
| 1049 | equal(environ.get("QUUX"), "bear"); |
| 1050 | equal(getenvInChild(pb,"QUUX"), "bear"); |
| 1051 | checkMapSanity(environ); |
| 1052 | |
| 1053 | // Remove env var |
| 1054 | environ.remove("QUUX"); |
| 1055 | equal(environ.get("QUUX"), null); |
| 1056 | equal(getenvInChild(pb,"QUUX"), "null"); |
| 1057 | checkMapSanity(environ); |
| 1058 | |
| 1059 | // Remove non-existent env var |
| 1060 | environ.remove("QUUX"); |
| 1061 | equal(environ.get("QUUX"), null); |
| 1062 | equal(getenvInChild(pb,"QUUX"), "null"); |
| 1063 | checkMapSanity(environ); |
| 1064 | } catch (Throwable t) { unexpected(t); } |
| 1065 | |
| 1066 | //---------------------------------------------------------------- |
| 1067 | // Pass Empty environment to child |
| 1068 | //---------------------------------------------------------------- |
| 1069 | try { |
| 1070 | ProcessBuilder pb = new ProcessBuilder(); |
| 1071 | pb.environment().clear(); |
| 1072 | equal(getenvInChild(pb), ""); |
| 1073 | } catch (Throwable t) { unexpected(t); } |
| 1074 | |
| 1075 | //---------------------------------------------------------------- |
| 1076 | // System.getenv() is read-only. |
| 1077 | //---------------------------------------------------------------- |
| 1078 | THROWS(UnsupportedOperationException.class, |
| 1079 | new Fun(){void f(){ getenv().put("FOO","BAR");}}, |
| 1080 | new Fun(){void f(){ getenv().remove("PATH");}}, |
| 1081 | new Fun(){void f(){ getenv().keySet().remove("PATH");}}, |
| 1082 | new Fun(){void f(){ getenv().values().remove("someValue");}}); |
| 1083 | |
| 1084 | try { |
| 1085 | Collection<Map.Entry<String,String>> c = getenv().entrySet(); |
| 1086 | if (! c.isEmpty()) |
| 1087 | try { |
| 1088 | c.iterator().next().setValue("foo"); |
| 1089 | fail("Expected UnsupportedOperationException not thrown"); |
| 1090 | } catch (UnsupportedOperationException e) {} // OK |
| 1091 | } catch (Throwable t) { unexpected(t); } |
| 1092 | |
| 1093 | //---------------------------------------------------------------- |
| 1094 | // System.getenv() always returns the same object in our implementation. |
| 1095 | //---------------------------------------------------------------- |
| 1096 | try { |
| 1097 | check(System.getenv() == System.getenv()); |
| 1098 | } catch (Throwable t) { unexpected(t); } |
| 1099 | |
| 1100 | //---------------------------------------------------------------- |
| 1101 | // You can't create an env var name containing "=", |
| 1102 | // or an env var name or value containing NUL. |
| 1103 | //---------------------------------------------------------------- |
| 1104 | { |
| 1105 | final Map<String,String> m = new ProcessBuilder().environment(); |
| 1106 | THROWS(IllegalArgumentException.class, |
| 1107 | new Fun(){void f(){ m.put("FOO=","BAR");}}, |
| 1108 | new Fun(){void f(){ m.put("FOO\u0000","BAR");}}, |
| 1109 | new Fun(){void f(){ m.put("FOO","BAR\u0000");}}); |
| 1110 | } |
| 1111 | |
| 1112 | //---------------------------------------------------------------- |
| 1113 | // Commands must never be null. |
| 1114 | //---------------------------------------------------------------- |
| 1115 | THROWS(NullPointerException.class, |
| 1116 | new Fun(){void f(){ |
| 1117 | new ProcessBuilder((List<String>)null);}}, |
| 1118 | new Fun(){void f(){ |
| 1119 | new ProcessBuilder().command((List<String>)null);}}); |
| 1120 | |
| 1121 | //---------------------------------------------------------------- |
| 1122 | // Put in a command; get the same one back out. |
| 1123 | //---------------------------------------------------------------- |
| 1124 | try { |
| 1125 | List<String> command = new ArrayList<String>(); |
| 1126 | ProcessBuilder pb = new ProcessBuilder(command); |
| 1127 | check(pb.command() == command); |
| 1128 | List<String> command2 = new ArrayList<String>(2); |
| 1129 | command2.add("foo"); |
| 1130 | command2.add("bar"); |
| 1131 | pb.command(command2); |
| 1132 | check(pb.command() == command2); |
| 1133 | pb.command("foo", "bar"); |
| 1134 | check(pb.command() != command2 && pb.command().equals(command2)); |
| 1135 | pb.command(command2); |
| 1136 | command2.add("baz"); |
| 1137 | equal(pb.command().get(2), "baz"); |
| 1138 | } catch (Throwable t) { unexpected(t); } |
| 1139 | |
| 1140 | //---------------------------------------------------------------- |
| 1141 | // Commands must contain at least one element. |
| 1142 | //---------------------------------------------------------------- |
| 1143 | THROWS(IndexOutOfBoundsException.class, |
| 1144 | new Fun() { void f() throws IOException { |
| 1145 | new ProcessBuilder().start();}}, |
| 1146 | new Fun() { void f() throws IOException { |
| 1147 | new ProcessBuilder(new ArrayList<String>()).start();}}, |
| 1148 | new Fun() { void f() throws IOException { |
| 1149 | Runtime.getRuntime().exec(new String[]{});}}); |
| 1150 | |
| 1151 | //---------------------------------------------------------------- |
| 1152 | // Commands must not contain null elements at start() time. |
| 1153 | //---------------------------------------------------------------- |
| 1154 | THROWS(NullPointerException.class, |
| 1155 | new Fun() { void f() throws IOException { |
| 1156 | new ProcessBuilder("foo",null,"bar").start();}}, |
| 1157 | new Fun() { void f() throws IOException { |
| 1158 | new ProcessBuilder((String)null).start();}}, |
| 1159 | new Fun() { void f() throws IOException { |
| 1160 | new ProcessBuilder(new String[]{null}).start();}}, |
| 1161 | new Fun() { void f() throws IOException { |
| 1162 | new ProcessBuilder(new String[]{"foo",null,"bar"}).start();}}); |
| 1163 | |
| 1164 | //---------------------------------------------------------------- |
| 1165 | // Command lists are growable. |
| 1166 | //---------------------------------------------------------------- |
| 1167 | try { |
| 1168 | new ProcessBuilder().command().add("foo"); |
| 1169 | new ProcessBuilder("bar").command().add("foo"); |
| 1170 | new ProcessBuilder(new String[]{"1","2"}).command().add("3"); |
| 1171 | } catch (Throwable t) { unexpected(t); } |
| 1172 | |
| 1173 | //---------------------------------------------------------------- |
| 1174 | // Nulls in environment updates generate NullPointerException |
| 1175 | //---------------------------------------------------------------- |
| 1176 | try { |
| 1177 | final Map<String,String> env = new ProcessBuilder().environment(); |
| 1178 | THROWS(NullPointerException.class, |
| 1179 | new Fun(){void f(){ env.put("foo",null);}}, |
| 1180 | new Fun(){void f(){ env.put(null,"foo");}}, |
| 1181 | new Fun(){void f(){ env.remove(null);}}, |
| 1182 | new Fun(){void f(){ |
| 1183 | for (Map.Entry<String,String> e : env.entrySet()) |
| 1184 | e.setValue(null);}}, |
| 1185 | new Fun() { void f() throws IOException { |
| 1186 | Runtime.getRuntime().exec(new String[]{"foo"}, |
| 1187 | new String[]{null});}}); |
| 1188 | } catch (Throwable t) { unexpected(t); } |
| 1189 | |
| 1190 | //---------------------------------------------------------------- |
| 1191 | // Non-String types in environment updates generate ClassCastException |
| 1192 | //---------------------------------------------------------------- |
| 1193 | try { |
| 1194 | final Map<String,String> env = new ProcessBuilder().environment(); |
| 1195 | THROWS(ClassCastException.class, |
| 1196 | new Fun(){void f(){ env.remove(TRUE);}}, |
| 1197 | new Fun(){void f(){ env.keySet().remove(TRUE);}}, |
| 1198 | new Fun(){void f(){ env.values().remove(TRUE);}}, |
| 1199 | new Fun(){void f(){ env.entrySet().remove(TRUE);}}); |
| 1200 | } catch (Throwable t) { unexpected(t); } |
| 1201 | |
| 1202 | //---------------------------------------------------------------- |
| 1203 | // Check query operations on environment maps |
| 1204 | //---------------------------------------------------------------- |
| 1205 | try { |
| 1206 | List<Map<String,String>> envs = |
| 1207 | new ArrayList<Map<String,String>>(2); |
| 1208 | envs.add(System.getenv()); |
| 1209 | envs.add(new ProcessBuilder().environment()); |
| 1210 | for (final Map<String,String> env : envs) { |
| 1211 | //---------------------------------------------------------------- |
| 1212 | // Nulls in environment queries are forbidden. |
| 1213 | //---------------------------------------------------------------- |
| 1214 | THROWS(NullPointerException.class, |
| 1215 | new Fun(){void f(){ getenv(null);}}, |
| 1216 | new Fun(){void f(){ env.get(null);}}, |
| 1217 | new Fun(){void f(){ env.containsKey(null);}}, |
| 1218 | new Fun(){void f(){ env.containsValue(null);}}, |
| 1219 | new Fun(){void f(){ env.keySet().contains(null);}}, |
| 1220 | new Fun(){void f(){ env.values().contains(null);}}); |
| 1221 | |
| 1222 | //---------------------------------------------------------------- |
| 1223 | // Non-String types in environment queries are forbidden. |
| 1224 | //---------------------------------------------------------------- |
| 1225 | THROWS(ClassCastException.class, |
| 1226 | new Fun(){void f(){ env.get(TRUE);}}, |
| 1227 | new Fun(){void f(){ env.containsKey(TRUE);}}, |
| 1228 | new Fun(){void f(){ env.containsValue(TRUE);}}, |
| 1229 | new Fun(){void f(){ env.keySet().contains(TRUE);}}, |
| 1230 | new Fun(){void f(){ env.values().contains(TRUE);}}); |
| 1231 | |
| 1232 | //---------------------------------------------------------------- |
| 1233 | // Illegal String values in environment queries are (grumble) OK |
| 1234 | //---------------------------------------------------------------- |
| 1235 | equal(env.get("\u0000"), null); |
| 1236 | check(! env.containsKey("\u0000")); |
| 1237 | check(! env.containsValue("\u0000")); |
| 1238 | check(! env.keySet().contains("\u0000")); |
| 1239 | check(! env.values().contains("\u0000")); |
| 1240 | } |
| 1241 | |
| 1242 | } catch (Throwable t) { unexpected(t); } |
| 1243 | |
| 1244 | try { |
| 1245 | final Set<Map.Entry<String,String>> entrySet = |
| 1246 | new ProcessBuilder().environment().entrySet(); |
| 1247 | THROWS(NullPointerException.class, |
| 1248 | new Fun(){void f(){ entrySet.contains(null);}}); |
| 1249 | THROWS(ClassCastException.class, |
| 1250 | new Fun(){void f(){ entrySet.contains(TRUE);}}, |
| 1251 | new Fun(){void f(){ |
| 1252 | entrySet.contains( |
| 1253 | new SimpleImmutableEntry<Boolean,String>(TRUE,""));}}); |
| 1254 | |
| 1255 | check(! entrySet.contains |
| 1256 | (new SimpleImmutableEntry<String,String>("", ""))); |
| 1257 | } catch (Throwable t) { unexpected(t); } |
| 1258 | |
| 1259 | //---------------------------------------------------------------- |
| 1260 | // Put in a directory; get the same one back out. |
| 1261 | //---------------------------------------------------------------- |
| 1262 | try { |
| 1263 | ProcessBuilder pb = new ProcessBuilder(); |
| 1264 | File foo = new File("foo"); |
| 1265 | equal(pb.directory(), null); |
| 1266 | equal(pb.directory(foo).directory(), foo); |
| 1267 | equal(pb.directory(null).directory(), null); |
| 1268 | } catch (Throwable t) { unexpected(t); } |
| 1269 | |
| 1270 | //---------------------------------------------------------------- |
| 1271 | // If round-trip conversion works, check envvar pass-through to child |
| 1272 | //---------------------------------------------------------------- |
| 1273 | try { |
| 1274 | testEncoding("ASCII", "xyzzy"); |
| 1275 | testEncoding("Latin1", "\u00f1\u00e1"); |
| 1276 | testEncoding("Unicode", "\u22f1\u11e1"); |
| 1277 | } catch (Throwable t) { unexpected(t); } |
| 1278 | |
| 1279 | //---------------------------------------------------------------- |
| 1280 | // A surprisingly large number of ways to delete an environment var. |
| 1281 | //---------------------------------------------------------------- |
| 1282 | testVariableDeleter(new EnvironmentFrobber() { |
| 1283 | public void doIt(Map<String,String> environ) { |
| 1284 | environ.remove("Foo");}}); |
| 1285 | |
| 1286 | testVariableDeleter(new EnvironmentFrobber() { |
| 1287 | public void doIt(Map<String,String> environ) { |
| 1288 | environ.keySet().remove("Foo");}}); |
| 1289 | |
| 1290 | testVariableDeleter(new EnvironmentFrobber() { |
| 1291 | public void doIt(Map<String,String> environ) { |
| 1292 | environ.values().remove("BAAR");}}); |
| 1293 | |
| 1294 | testVariableDeleter(new EnvironmentFrobber() { |
| 1295 | public void doIt(Map<String,String> environ) { |
| 1296 | // Legally fabricate a ProcessEnvironment.StringEntry, |
| 1297 | // even though it's private. |
| 1298 | Map<String,String> environ2 |
| 1299 | = new ProcessBuilder().environment(); |
| 1300 | environ2.clear(); |
| 1301 | environ2.put("Foo","BAAR"); |
| 1302 | // Subtlety alert. |
| 1303 | Map.Entry<String,String> e |
| 1304 | = environ2.entrySet().iterator().next(); |
| 1305 | environ.entrySet().remove(e);}}); |
| 1306 | |
| 1307 | testVariableDeleter(new EnvironmentFrobber() { |
| 1308 | public void doIt(Map<String,String> environ) { |
| 1309 | Map.Entry<String,String> victim = null; |
| 1310 | for (Map.Entry<String,String> e : environ.entrySet()) |
| 1311 | if (e.getKey().equals("Foo")) |
| 1312 | victim = e; |
| 1313 | if (victim != null) |
| 1314 | environ.entrySet().remove(victim);}}); |
| 1315 | |
| 1316 | testVariableDeleter(new EnvironmentFrobber() { |
| 1317 | public void doIt(Map<String,String> environ) { |
| 1318 | Iterator<String> it = environ.keySet().iterator(); |
| 1319 | while (it.hasNext()) { |
| 1320 | String val = it.next(); |
| 1321 | if (val.equals("Foo")) |
| 1322 | it.remove();}}}); |
| 1323 | |
| 1324 | testVariableDeleter(new EnvironmentFrobber() { |
| 1325 | public void doIt(Map<String,String> environ) { |
| 1326 | Iterator<Map.Entry<String,String>> it |
| 1327 | = environ.entrySet().iterator(); |
| 1328 | while (it.hasNext()) { |
| 1329 | Map.Entry<String,String> e = it.next(); |
| 1330 | if (e.getKey().equals("Foo")) |
| 1331 | it.remove();}}}); |
| 1332 | |
| 1333 | testVariableDeleter(new EnvironmentFrobber() { |
| 1334 | public void doIt(Map<String,String> environ) { |
| 1335 | Iterator<String> it = environ.values().iterator(); |
| 1336 | while (it.hasNext()) { |
| 1337 | String val = it.next(); |
| 1338 | if (val.equals("BAAR")) |
| 1339 | it.remove();}}}); |
| 1340 | |
| 1341 | //---------------------------------------------------------------- |
| 1342 | // A surprisingly small number of ways to add an environment var. |
| 1343 | //---------------------------------------------------------------- |
| 1344 | testVariableAdder(new EnvironmentFrobber() { |
| 1345 | public void doIt(Map<String,String> environ) { |
| 1346 | environ.put("Foo","Bahrein");}}); |
| 1347 | |
| 1348 | //---------------------------------------------------------------- |
| 1349 | // A few ways to modify an environment var. |
| 1350 | //---------------------------------------------------------------- |
| 1351 | testVariableModifier(new EnvironmentFrobber() { |
| 1352 | public void doIt(Map<String,String> environ) { |
| 1353 | environ.put("Foo","NewValue");}}); |
| 1354 | |
| 1355 | testVariableModifier(new EnvironmentFrobber() { |
| 1356 | public void doIt(Map<String,String> environ) { |
| 1357 | for (Map.Entry<String,String> e : environ.entrySet()) |
| 1358 | if (e.getKey().equals("Foo")) |
| 1359 | e.setValue("NewValue");}}); |
| 1360 | |
| 1361 | //---------------------------------------------------------------- |
| 1362 | // Fiddle with environment sizes |
| 1363 | //---------------------------------------------------------------- |
| 1364 | try { |
| 1365 | Map<String,String> environ = new ProcessBuilder().environment(); |
| 1366 | int size = environ.size(); |
| 1367 | checkSizes(environ, size); |
| 1368 | |
| 1369 | environ.put("UnLiKeLYeNVIROmtNam", "someVal"); |
| 1370 | checkSizes(environ, size+1); |
| 1371 | |
| 1372 | // Check for environment independence |
| 1373 | new ProcessBuilder().environment().clear(); |
| 1374 | |
| 1375 | environ.put("UnLiKeLYeNVIROmtNam", "someOtherVal"); |
| 1376 | checkSizes(environ, size+1); |
| 1377 | |
| 1378 | environ.remove("UnLiKeLYeNVIROmtNam"); |
| 1379 | checkSizes(environ, size); |
| 1380 | |
| 1381 | environ.clear(); |
| 1382 | checkSizes(environ, 0); |
| 1383 | |
| 1384 | environ.clear(); |
| 1385 | checkSizes(environ, 0); |
| 1386 | |
| 1387 | environ = new ProcessBuilder().environment(); |
| 1388 | environ.keySet().clear(); |
| 1389 | checkSizes(environ, 0); |
| 1390 | |
| 1391 | environ = new ProcessBuilder().environment(); |
| 1392 | environ.entrySet().clear(); |
| 1393 | checkSizes(environ, 0); |
| 1394 | |
| 1395 | environ = new ProcessBuilder().environment(); |
| 1396 | environ.values().clear(); |
| 1397 | checkSizes(environ, 0); |
| 1398 | } catch (Throwable t) { unexpected(t); } |
| 1399 | |
| 1400 | //---------------------------------------------------------------- |
| 1401 | // Check that various map invariants hold |
| 1402 | //---------------------------------------------------------------- |
| 1403 | checkMapSanity(new ProcessBuilder().environment()); |
| 1404 | checkMapSanity(System.getenv()); |
| 1405 | checkMapEquality(new ProcessBuilder().environment(), |
| 1406 | new ProcessBuilder().environment()); |
| 1407 | |
| 1408 | |
| 1409 | //---------------------------------------------------------------- |
| 1410 | // Check effects on external "env" command. |
| 1411 | //---------------------------------------------------------------- |
| 1412 | try { |
| 1413 | Set<String> env1 = new HashSet<String> |
| 1414 | (Arrays.asList(nativeEnv((String[])null).split("\n"))); |
| 1415 | |
| 1416 | ProcessBuilder pb = new ProcessBuilder(); |
| 1417 | pb.environment().put("QwErTyUiOp","AsDfGhJk"); |
| 1418 | |
| 1419 | Set<String> env2 = new HashSet<String> |
| 1420 | (Arrays.asList(nativeEnv(pb).split("\n"))); |
| 1421 | |
| 1422 | check(env2.size() == env1.size() + 1); |
| 1423 | env1.add("QwErTyUiOp=AsDfGhJk"); |
| 1424 | check(env1.equals(env2)); |
| 1425 | } catch (Throwable t) { unexpected(t); } |
| 1426 | |
| 1427 | //---------------------------------------------------------------- |
| 1428 | // Test Runtime.exec(...envp...) |
| 1429 | // Check for sort order of environment variables on Windows. |
| 1430 | //---------------------------------------------------------------- |
| 1431 | try { |
| 1432 | // '+' < 'A' < 'Z' < '_' < 'a' < 'z' < '~' |
| 1433 | String[]envp = {"FOO=BAR","BAZ=GORP","QUUX=", |
| 1434 | "+=+", "_=_", "~=~"}; |
| 1435 | String output = nativeEnv(envp); |
| 1436 | String expected = "+=+\nBAZ=GORP\nFOO=BAR\nQUUX=\n_=_\n~=~\n"; |
| 1437 | // On Windows, Java must keep the environment sorted. |
| 1438 | // Order is random on Unix, so this test does the sort. |
| 1439 | if (! Windows.is()) |
| 1440 | output = sortByLinesWindowsly(output); |
| 1441 | equal(output, expected); |
| 1442 | } catch (Throwable t) { unexpected(t); } |
| 1443 | |
| 1444 | //---------------------------------------------------------------- |
| 1445 | // System.getenv() must be consistent with System.getenv(String) |
| 1446 | //---------------------------------------------------------------- |
| 1447 | try { |
| 1448 | for (Map.Entry<String,String> e : getenv().entrySet()) |
| 1449 | equal(getenv(e.getKey()), e.getValue()); |
| 1450 | } catch (Throwable t) { unexpected(t); } |
| 1451 | |
| 1452 | //---------------------------------------------------------------- |
| 1453 | // Fiddle with working directory in child |
| 1454 | //---------------------------------------------------------------- |
| 1455 | try { |
| 1456 | String canonicalUserDir = |
| 1457 | new File(System.getProperty("user.dir")).getCanonicalPath(); |
| 1458 | String[] sdirs = new String[] |
| 1459 | {".", "..", "/", "/bin", |
sherman | 49c3dc4 | 2010-04-03 18:29:11 -0700 | [diff] [blame] | 1460 | "C:", "c:", "C:/", "c:\\", "\\", "\\bin", |
| 1461 | "c:\\windows ", "c:\\Program Files", "c:\\Program Files\\" }; |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1462 | for (String sdir : sdirs) { |
| 1463 | File dir = new File(sdir); |
| 1464 | if (! (dir.isDirectory() && dir.exists())) |
| 1465 | continue; |
| 1466 | out.println("Testing directory " + dir); |
sherman | 49c3dc4 | 2010-04-03 18:29:11 -0700 | [diff] [blame] | 1467 | //dir = new File(dir.getCanonicalPath()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1468 | |
| 1469 | ProcessBuilder pb = new ProcessBuilder(); |
| 1470 | equal(pb.directory(), null); |
| 1471 | equal(pwdInChild(pb), canonicalUserDir); |
| 1472 | |
| 1473 | pb.directory(dir); |
| 1474 | equal(pb.directory(), dir); |
sherman | 49c3dc4 | 2010-04-03 18:29:11 -0700 | [diff] [blame] | 1475 | equal(pwdInChild(pb), dir.getCanonicalPath()); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1476 | |
| 1477 | pb.directory(null); |
| 1478 | equal(pb.directory(), null); |
| 1479 | equal(pwdInChild(pb), canonicalUserDir); |
| 1480 | |
| 1481 | pb.directory(dir); |
| 1482 | } |
| 1483 | } catch (Throwable t) { unexpected(t); } |
| 1484 | |
| 1485 | //---------------------------------------------------------------- |
sherman | 49c3dc4 | 2010-04-03 18:29:11 -0700 | [diff] [blame] | 1486 | // Working directory with Unicode in child |
| 1487 | //---------------------------------------------------------------- |
| 1488 | try { |
| 1489 | if (UnicodeOS.is()) { |
| 1490 | File dir = new File(System.getProperty("test.dir", "."), |
| 1491 | "ProcessBuilderDir\u4e00\u4e02"); |
| 1492 | try { |
| 1493 | if (!dir.exists()) |
| 1494 | dir.mkdir(); |
| 1495 | out.println("Testing Unicode directory:" + dir); |
| 1496 | ProcessBuilder pb = new ProcessBuilder(); |
| 1497 | pb.directory(dir); |
| 1498 | equal(pwdInChild(pb), dir.getCanonicalPath()); |
| 1499 | } finally { |
| 1500 | if (dir.exists()) |
| 1501 | dir.delete(); |
| 1502 | } |
| 1503 | } |
| 1504 | } catch (Throwable t) { unexpected(t); } |
| 1505 | |
| 1506 | //---------------------------------------------------------------- |
martin | 07f8b42 | 2009-09-08 14:33:59 -0700 | [diff] [blame] | 1507 | // OOME in child allocating maximally sized array |
| 1508 | // Test for hotspot/jvmti bug 6850957 |
| 1509 | //---------------------------------------------------------------- |
| 1510 | try { |
| 1511 | List<String> list = new ArrayList<String>(javaChildArgs); |
| 1512 | list.add(1, String.format("-XX:OnOutOfMemoryError=%s -version", |
| 1513 | javaExe)); |
| 1514 | list.add("ArrayOOME"); |
| 1515 | ProcessResults r = run(new ProcessBuilder(list)); |
| 1516 | check(r.out().contains("java.lang.OutOfMemoryError:")); |
| 1517 | check(r.out().contains(javaExe)); |
| 1518 | check(r.err().contains(System.getProperty("java.version"))); |
| 1519 | equal(r.exitValue(), 1); |
| 1520 | } catch (Throwable t) { unexpected(t); } |
| 1521 | |
| 1522 | //---------------------------------------------------------------- |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1523 | // Windows has tricky semi-case-insensitive semantics |
| 1524 | //---------------------------------------------------------------- |
| 1525 | if (Windows.is()) |
| 1526 | try { |
| 1527 | out.println("Running case insensitve variable tests"); |
| 1528 | for (String[] namePair : |
| 1529 | new String[][] |
| 1530 | { new String[]{"PATH","PaTh"}, |
| 1531 | new String[]{"home","HOME"}, |
| 1532 | new String[]{"SYSTEMROOT","SystemRoot"}}) { |
| 1533 | check((getenv(namePair[0]) == null && |
| 1534 | getenv(namePair[1]) == null) |
| 1535 | || |
| 1536 | getenv(namePair[0]).equals(getenv(namePair[1])), |
| 1537 | "Windows environment variables are not case insensitive"); |
| 1538 | } |
| 1539 | } catch (Throwable t) { unexpected(t); } |
| 1540 | |
| 1541 | //---------------------------------------------------------------- |
| 1542 | // Test proper Unicode child environment transfer |
| 1543 | //---------------------------------------------------------------- |
| 1544 | if (UnicodeOS.is()) |
| 1545 | try { |
| 1546 | ProcessBuilder pb = new ProcessBuilder(); |
| 1547 | pb.environment().put("\u1234","\u5678"); |
| 1548 | pb.environment().remove("PATH"); |
| 1549 | equal(getenvInChild1234(pb), "\u5678"); |
| 1550 | } catch (Throwable t) { unexpected(t); } |
| 1551 | |
| 1552 | |
| 1553 | //---------------------------------------------------------------- |
| 1554 | // Test Runtime.exec(...envp...) with envstrings with initial `=' |
| 1555 | //---------------------------------------------------------------- |
| 1556 | try { |
| 1557 | List<String> childArgs = new ArrayList<String>(javaChildArgs); |
| 1558 | childArgs.add("System.getenv()"); |
| 1559 | String[] cmdp = childArgs.toArray(new String[childArgs.size()]); |
| 1560 | String[] envp = {"=ExitValue=3", "=C:=\\"}; |
| 1561 | Process p = Runtime.getRuntime().exec(cmdp, envp); |
| 1562 | String expected = Windows.is() ? "=C:=\\,=ExitValue=3," : "=C:=\\,"; |
| 1563 | equal(commandOutput(p), expected); |
| 1564 | if (Windows.is()) { |
| 1565 | ProcessBuilder pb = new ProcessBuilder(childArgs); |
| 1566 | pb.environment().clear(); |
| 1567 | pb.environment().put("=ExitValue", "3"); |
| 1568 | pb.environment().put("=C:", "\\"); |
| 1569 | equal(commandOutput(pb), expected); |
| 1570 | } |
| 1571 | } catch (Throwable t) { unexpected(t); } |
| 1572 | |
| 1573 | //---------------------------------------------------------------- |
| 1574 | // Test Runtime.exec(...envp...) with envstrings without any `=' |
| 1575 | //---------------------------------------------------------------- |
| 1576 | try { |
| 1577 | String[] cmdp = {"echo"}; |
| 1578 | String[] envp = {"Hello", "World"}; // Yuck! |
| 1579 | Process p = Runtime.getRuntime().exec(cmdp, envp); |
| 1580 | equal(commandOutput(p), "\n"); |
| 1581 | } catch (Throwable t) { unexpected(t); } |
| 1582 | |
| 1583 | //---------------------------------------------------------------- |
| 1584 | // Test Runtime.exec(...envp...) with envstrings containing NULs |
| 1585 | //---------------------------------------------------------------- |
| 1586 | try { |
| 1587 | List<String> childArgs = new ArrayList<String>(javaChildArgs); |
| 1588 | childArgs.add("System.getenv()"); |
| 1589 | String[] cmdp = childArgs.toArray(new String[childArgs.size()]); |
| 1590 | String[] envp = {"LC_ALL=C\u0000\u0000", // Yuck! |
| 1591 | "FO\u0000=B\u0000R"}; |
| 1592 | Process p = Runtime.getRuntime().exec(cmdp, envp); |
| 1593 | check(commandOutput(p).equals("LC_ALL=C,"), |
| 1594 | "Incorrect handling of envstrings containing NULs"); |
| 1595 | } catch (Throwable t) { unexpected(t); } |
| 1596 | |
| 1597 | //---------------------------------------------------------------- |
| 1598 | // Test the redirectErrorStream property |
| 1599 | //---------------------------------------------------------------- |
| 1600 | try { |
| 1601 | ProcessBuilder pb = new ProcessBuilder(); |
| 1602 | equal(pb.redirectErrorStream(), false); |
| 1603 | equal(pb.redirectErrorStream(true), pb); |
| 1604 | equal(pb.redirectErrorStream(), true); |
| 1605 | equal(pb.redirectErrorStream(false), pb); |
| 1606 | equal(pb.redirectErrorStream(), false); |
| 1607 | } catch (Throwable t) { unexpected(t); } |
| 1608 | |
| 1609 | try { |
| 1610 | List<String> childArgs = new ArrayList<String>(javaChildArgs); |
| 1611 | childArgs.add("OutErr"); |
| 1612 | ProcessBuilder pb = new ProcessBuilder(childArgs); |
| 1613 | { |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 1614 | ProcessResults r = run(pb); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1615 | equal(r.out(), "outout"); |
| 1616 | equal(r.err(), "errerr"); |
| 1617 | } |
| 1618 | { |
| 1619 | pb.redirectErrorStream(true); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 1620 | ProcessResults r = run(pb); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1621 | equal(r.out(), "outerrouterr"); |
| 1622 | equal(r.err(), ""); |
| 1623 | } |
| 1624 | } catch (Throwable t) { unexpected(t); } |
| 1625 | |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 1626 | if (Unix.is()) { |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1627 | //---------------------------------------------------------------- |
| 1628 | // We can find true and false when PATH is null |
| 1629 | //---------------------------------------------------------------- |
| 1630 | try { |
| 1631 | List<String> childArgs = new ArrayList<String>(javaChildArgs); |
| 1632 | childArgs.add("null PATH"); |
| 1633 | ProcessBuilder pb = new ProcessBuilder(childArgs); |
| 1634 | pb.environment().remove("PATH"); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 1635 | ProcessResults r = run(pb); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1636 | equal(r.out(), ""); |
| 1637 | equal(r.err(), ""); |
| 1638 | equal(r.exitValue(), 0); |
| 1639 | } catch (Throwable t) { unexpected(t); } |
| 1640 | |
| 1641 | //---------------------------------------------------------------- |
| 1642 | // PATH search algorithm on Unix |
| 1643 | //---------------------------------------------------------------- |
| 1644 | try { |
| 1645 | List<String> childArgs = new ArrayList<String>(javaChildArgs); |
| 1646 | childArgs.add("PATH search algorithm"); |
| 1647 | ProcessBuilder pb = new ProcessBuilder(childArgs); |
| 1648 | pb.environment().put("PATH", "dir1:dir2:"); |
martin | 2ea07df | 2009-06-14 14:23:22 -0700 | [diff] [blame] | 1649 | ProcessResults r = run(pb); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1650 | equal(r.out(), ""); |
| 1651 | equal(r.err(), ""); |
| 1652 | equal(r.exitValue(), True.exitValue()); |
| 1653 | } catch (Throwable t) { unexpected(t); } |
| 1654 | |
| 1655 | //---------------------------------------------------------------- |
| 1656 | // Parent's, not child's PATH is used |
| 1657 | //---------------------------------------------------------------- |
| 1658 | try { |
| 1659 | new File("suBdiR").mkdirs(); |
| 1660 | copy("/bin/true", "suBdiR/unliKely"); |
| 1661 | final ProcessBuilder pb = |
| 1662 | new ProcessBuilder(new String[]{"unliKely"}); |
| 1663 | pb.environment().put("PATH", "suBdiR"); |
| 1664 | THROWS(IOException.class, |
| 1665 | new Fun() {void f() throws Throwable {pb.start();}}); |
| 1666 | } catch (Throwable t) { unexpected(t); |
| 1667 | } finally { |
| 1668 | new File("suBdiR/unliKely").delete(); |
| 1669 | new File("suBdiR").delete(); |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | //---------------------------------------------------------------- |
| 1674 | // Attempt to start bogus program "" |
| 1675 | //---------------------------------------------------------------- |
| 1676 | try { |
| 1677 | new ProcessBuilder("").start(); |
| 1678 | fail("Expected IOException not thrown"); |
| 1679 | } catch (IOException e) { |
| 1680 | String m = e.getMessage(); |
| 1681 | if (EnglishUnix.is() && |
| 1682 | ! matches(m, "No such file or directory")) |
| 1683 | unexpected(e); |
| 1684 | } catch (Throwable t) { unexpected(t); } |
| 1685 | |
| 1686 | //---------------------------------------------------------------- |
| 1687 | // Check that attempt to execute program name with funny |
| 1688 | // characters throws an exception containing those characters. |
| 1689 | //---------------------------------------------------------------- |
| 1690 | for (String programName : new String[] {"\u00f0", "\u01f0"}) |
| 1691 | try { |
| 1692 | new ProcessBuilder(programName).start(); |
| 1693 | fail("Expected IOException not thrown"); |
| 1694 | } catch (IOException e) { |
| 1695 | String m = e.getMessage(); |
| 1696 | Pattern p = Pattern.compile(programName); |
| 1697 | if (! matches(m, programName) |
| 1698 | || (EnglishUnix.is() |
| 1699 | && ! matches(m, "No such file or directory"))) |
| 1700 | unexpected(e); |
| 1701 | } catch (Throwable t) { unexpected(t); } |
| 1702 | |
| 1703 | //---------------------------------------------------------------- |
| 1704 | // Attempt to start process in nonexistent directory fails. |
| 1705 | //---------------------------------------------------------------- |
| 1706 | try { |
| 1707 | new ProcessBuilder("echo") |
| 1708 | .directory(new File("UnLiKeLY")) |
| 1709 | .start(); |
| 1710 | fail("Expected IOException not thrown"); |
| 1711 | } catch (IOException e) { |
| 1712 | String m = e.getMessage(); |
| 1713 | if (! matches(m, "in directory") |
| 1714 | || (EnglishUnix.is() && |
| 1715 | ! matches(m, "No such file or directory"))) |
| 1716 | unexpected(e); |
| 1717 | } catch (Throwable t) { unexpected(t); } |
| 1718 | |
| 1719 | //---------------------------------------------------------------- |
| 1720 | // This would deadlock, if not for the fact that |
| 1721 | // interprocess pipe buffers are at least 4096 bytes. |
| 1722 | //---------------------------------------------------------------- |
| 1723 | try { |
| 1724 | List<String> childArgs = new ArrayList<String>(javaChildArgs); |
| 1725 | childArgs.add("print4095"); |
| 1726 | Process p = new ProcessBuilder(childArgs).start(); |
| 1727 | print4095(p.getOutputStream()); // Might hang! |
| 1728 | p.waitFor(); // Might hang! |
| 1729 | equal(p.exitValue(), 5); |
| 1730 | } catch (Throwable t) { unexpected(t); } |
| 1731 | |
| 1732 | //---------------------------------------------------------------- |
| 1733 | // Attempt to start process with insufficient permissions fails. |
| 1734 | //---------------------------------------------------------------- |
| 1735 | try { |
| 1736 | new File("emptyCommand").delete(); |
| 1737 | new FileOutputStream("emptyCommand").close(); |
| 1738 | new File("emptyCommand").setExecutable(false); |
| 1739 | new ProcessBuilder("./emptyCommand").start(); |
| 1740 | fail("Expected IOException not thrown"); |
| 1741 | } catch (IOException e) { |
| 1742 | new File("./emptyCommand").delete(); |
| 1743 | String m = e.getMessage(); |
| 1744 | //e.printStackTrace(); |
| 1745 | if (EnglishUnix.is() && |
| 1746 | ! matches(m, "Permission denied")) |
| 1747 | unexpected(e); |
| 1748 | } catch (Throwable t) { unexpected(t); } |
| 1749 | |
| 1750 | new File("emptyCommand").delete(); |
| 1751 | |
| 1752 | //---------------------------------------------------------------- |
| 1753 | // Check for correct security permission behavior |
| 1754 | //---------------------------------------------------------------- |
| 1755 | final Policy policy = new Policy(); |
| 1756 | Policy.setPolicy(policy); |
| 1757 | System.setSecurityManager(new SecurityManager()); |
| 1758 | |
| 1759 | try { |
| 1760 | // No permissions required to CREATE a ProcessBuilder |
| 1761 | policy.setPermissions(/* Nothing */); |
| 1762 | new ProcessBuilder("env").directory(null).directory(); |
| 1763 | new ProcessBuilder("env").directory(new File("dir")).directory(); |
| 1764 | new ProcessBuilder("env").command("??").command(); |
| 1765 | } catch (Throwable t) { unexpected(t); } |
| 1766 | |
| 1767 | THROWS(SecurityException.class, |
| 1768 | new Fun() { void f() throws IOException { |
| 1769 | policy.setPermissions(/* Nothing */); |
| 1770 | System.getenv("foo");}}, |
| 1771 | new Fun() { void f() throws IOException { |
| 1772 | policy.setPermissions(/* Nothing */); |
| 1773 | System.getenv();}}, |
| 1774 | new Fun() { void f() throws IOException { |
| 1775 | policy.setPermissions(/* Nothing */); |
| 1776 | new ProcessBuilder("echo").start();}}, |
| 1777 | new Fun() { void f() throws IOException { |
| 1778 | policy.setPermissions(/* Nothing */); |
| 1779 | Runtime.getRuntime().exec("echo");}}, |
| 1780 | new Fun() { void f() throws IOException { |
| 1781 | policy.setPermissions(new RuntimePermission("getenv.bar")); |
| 1782 | System.getenv("foo");}}); |
| 1783 | |
| 1784 | try { |
| 1785 | policy.setPermissions(new RuntimePermission("getenv.foo")); |
| 1786 | System.getenv("foo"); |
| 1787 | |
| 1788 | policy.setPermissions(new RuntimePermission("getenv.*")); |
| 1789 | System.getenv("foo"); |
| 1790 | System.getenv(); |
| 1791 | new ProcessBuilder().environment(); |
| 1792 | } catch (Throwable t) { unexpected(t); } |
| 1793 | |
| 1794 | |
| 1795 | final Permission execPermission |
| 1796 | = new FilePermission("<<ALL FILES>>", "execute"); |
| 1797 | |
| 1798 | THROWS(SecurityException.class, |
| 1799 | new Fun() { void f() throws IOException { |
| 1800 | // environment permission by itself insufficient |
| 1801 | policy.setPermissions(new RuntimePermission("getenv.*")); |
| 1802 | ProcessBuilder pb = new ProcessBuilder("env"); |
| 1803 | pb.environment().put("foo","bar"); |
| 1804 | pb.start();}}, |
| 1805 | new Fun() { void f() throws IOException { |
| 1806 | // exec permission by itself insufficient |
| 1807 | policy.setPermissions(execPermission); |
| 1808 | ProcessBuilder pb = new ProcessBuilder("env"); |
| 1809 | pb.environment().put("foo","bar"); |
| 1810 | pb.start();}}); |
| 1811 | |
| 1812 | try { |
| 1813 | // Both permissions? OK. |
| 1814 | policy.setPermissions(new RuntimePermission("getenv.*"), |
| 1815 | execPermission); |
| 1816 | ProcessBuilder pb = new ProcessBuilder("env"); |
| 1817 | pb.environment().put("foo","bar"); |
martin | dd2fd9f | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 1818 | Process p = pb.start(); |
| 1819 | closeStreams(p); |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1820 | } catch (IOException e) { // OK |
| 1821 | } catch (Throwable t) { unexpected(t); } |
| 1822 | |
| 1823 | try { |
| 1824 | // Don't need environment permission unless READING environment |
| 1825 | policy.setPermissions(execPermission); |
| 1826 | Runtime.getRuntime().exec("env", new String[]{}); |
| 1827 | } catch (IOException e) { // OK |
| 1828 | } catch (Throwable t) { unexpected(t); } |
| 1829 | |
| 1830 | try { |
| 1831 | // Don't need environment permission unless READING environment |
| 1832 | policy.setPermissions(execPermission); |
| 1833 | new ProcessBuilder("env").start(); |
| 1834 | } catch (IOException e) { // OK |
| 1835 | } catch (Throwable t) { unexpected(t); } |
| 1836 | |
| 1837 | // Restore "normal" state without a security manager |
| 1838 | policy.setPermissions(new RuntimePermission("setSecurityManager")); |
| 1839 | System.setSecurityManager(null); |
| 1840 | |
| 1841 | } |
| 1842 | |
martin | dd2fd9f | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 1843 | static void closeStreams(Process p) { |
| 1844 | try { |
| 1845 | p.getOutputStream().close(); |
| 1846 | p.getInputStream().close(); |
| 1847 | p.getErrorStream().close(); |
| 1848 | } catch (Throwable t) { unexpected(t); } |
| 1849 | } |
| 1850 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1851 | //---------------------------------------------------------------- |
| 1852 | // A Policy class designed to make permissions fiddling very easy. |
| 1853 | //---------------------------------------------------------------- |
| 1854 | private static class Policy extends java.security.Policy { |
| 1855 | private Permissions perms; |
| 1856 | |
| 1857 | public void setPermissions(Permission...permissions) { |
| 1858 | perms = new Permissions(); |
| 1859 | for (Permission permission : permissions) |
| 1860 | perms.add(permission); |
| 1861 | } |
| 1862 | |
| 1863 | public Policy() { setPermissions(/* Nothing */); } |
| 1864 | |
| 1865 | public PermissionCollection getPermissions(CodeSource cs) { |
| 1866 | return perms; |
| 1867 | } |
| 1868 | |
| 1869 | public PermissionCollection getPermissions(ProtectionDomain pd) { |
| 1870 | return perms; |
| 1871 | } |
| 1872 | |
| 1873 | public boolean implies(ProtectionDomain pd, Permission p) { |
| 1874 | return perms.implies(p); |
| 1875 | } |
| 1876 | |
| 1877 | public void refresh() {} |
| 1878 | } |
| 1879 | |
| 1880 | private static class StreamAccumulator extends Thread { |
| 1881 | private final InputStream is; |
| 1882 | private final StringBuilder sb = new StringBuilder(); |
| 1883 | private Throwable throwable = null; |
| 1884 | |
| 1885 | public String result () throws Throwable { |
| 1886 | if (throwable != null) |
| 1887 | throw throwable; |
| 1888 | return sb.toString(); |
| 1889 | } |
| 1890 | |
| 1891 | StreamAccumulator (InputStream is) { |
| 1892 | this.is = is; |
| 1893 | } |
| 1894 | |
| 1895 | public void run() { |
| 1896 | try { |
| 1897 | Reader r = new InputStreamReader(is); |
| 1898 | char[] buf = new char[4096]; |
| 1899 | int n; |
| 1900 | while ((n = r.read(buf)) > 0) { |
| 1901 | sb.append(buf,0,n); |
| 1902 | } |
| 1903 | } catch (Throwable t) { |
| 1904 | throwable = t; |
martin | dd2fd9f | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 1905 | } finally { |
| 1906 | try { is.close(); } |
| 1907 | catch (Throwable t) { throwable = t; } |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
martin | dd2fd9f | 2008-03-10 14:32:51 -0700 | [diff] [blame] | 1912 | static ProcessResults run(ProcessBuilder pb) { |
| 1913 | try { |
| 1914 | return run(pb.start()); |
| 1915 | } catch (Throwable t) { unexpected(t); return null; } |
| 1916 | } |
| 1917 | |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1918 | private static ProcessResults run(Process p) { |
| 1919 | Throwable throwable = null; |
| 1920 | int exitValue = -1; |
| 1921 | String out = ""; |
| 1922 | String err = ""; |
| 1923 | |
| 1924 | StreamAccumulator outAccumulator = |
| 1925 | new StreamAccumulator(p.getInputStream()); |
| 1926 | StreamAccumulator errAccumulator = |
| 1927 | new StreamAccumulator(p.getErrorStream()); |
| 1928 | |
| 1929 | try { |
| 1930 | outAccumulator.start(); |
| 1931 | errAccumulator.start(); |
| 1932 | |
| 1933 | exitValue = p.waitFor(); |
| 1934 | |
| 1935 | outAccumulator.join(); |
| 1936 | errAccumulator.join(); |
| 1937 | |
| 1938 | out = outAccumulator.result(); |
| 1939 | err = errAccumulator.result(); |
| 1940 | } catch (Throwable t) { |
| 1941 | throwable = t; |
| 1942 | } |
| 1943 | |
| 1944 | return new ProcessResults(out, err, exitValue, throwable); |
| 1945 | } |
| 1946 | |
| 1947 | //---------------------------------------------------------------- |
| 1948 | // Results of a command |
| 1949 | //---------------------------------------------------------------- |
| 1950 | private static class ProcessResults { |
| 1951 | private final String out; |
| 1952 | private final String err; |
| 1953 | private final int exitValue; |
| 1954 | private final Throwable throwable; |
| 1955 | |
| 1956 | public ProcessResults(String out, |
| 1957 | String err, |
| 1958 | int exitValue, |
| 1959 | Throwable throwable) { |
| 1960 | this.out = out; |
| 1961 | this.err = err; |
| 1962 | this.exitValue = exitValue; |
| 1963 | this.throwable = throwable; |
| 1964 | } |
| 1965 | |
| 1966 | public String out() { return out; } |
| 1967 | public String err() { return err; } |
| 1968 | public int exitValue() { return exitValue; } |
| 1969 | public Throwable throwable() { return throwable; } |
| 1970 | |
| 1971 | public String toString() { |
| 1972 | StringBuilder sb = new StringBuilder(); |
| 1973 | sb.append("<STDOUT>\n" + out() + "</STDOUT>\n") |
| 1974 | .append("<STDERR>\n" + err() + "</STDERR>\n") |
| 1975 | .append("exitValue = " + exitValue + "\n"); |
| 1976 | if (throwable != null) |
| 1977 | sb.append(throwable.getStackTrace()); |
| 1978 | return sb.toString(); |
| 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | //--------------------- Infrastructure --------------------------- |
| 1983 | static volatile int passed = 0, failed = 0; |
| 1984 | static void pass() {passed++;} |
| 1985 | static void fail() {failed++; Thread.dumpStack();} |
| 1986 | static void fail(String msg) {System.out.println(msg); fail();} |
| 1987 | static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 1988 | static void check(boolean cond) {if (cond) pass(); else fail();} |
| 1989 | static void check(boolean cond, String m) {if (cond) pass(); else fail(m);} |
| 1990 | static void equal(Object x, Object y) { |
| 1991 | if (x == null ? y == null : x.equals(y)) pass(); |
| 1992 | else fail(x + " not equal to " + y);} |
| 1993 | public static void main(String[] args) throws Throwable { |
| 1994 | try {realMain(args);} catch (Throwable t) {unexpected(t);} |
| 1995 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 1996 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 1997 | private static abstract class Fun {abstract void f() throws Throwable;} |
| 1998 | static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
| 1999 | for (Fun f : fs) |
| 2000 | try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
| 2001 | catch (Throwable t) { |
| 2002 | if (k.isAssignableFrom(t.getClass())) pass(); |
| 2003 | else unexpected(t);}} |
| 2004 | } |