2
|
1 |
/*
|
|
2 |
* Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
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
|
|
28 |
* 6464154 6523983 6206031
|
|
29 |
* @summary Basic tests for Process and Environment Variable code
|
|
30 |
* @run main/othervm Basic
|
|
31 |
* @author Martin Buchholz
|
|
32 |
*/
|
|
33 |
|
|
34 |
import java.io.*;
|
|
35 |
import java.util.*;
|
|
36 |
import java.security.*;
|
|
37 |
import java.util.regex.Pattern;
|
|
38 |
import static java.lang.System.getenv;
|
|
39 |
import static java.lang.System.out;
|
|
40 |
import static java.lang.Boolean.TRUE;
|
|
41 |
import static java.util.AbstractMap.SimpleImmutableEntry;
|
|
42 |
|
|
43 |
public class Basic {
|
|
44 |
|
|
45 |
private static String commandOutput(Reader r) throws Throwable {
|
|
46 |
StringBuilder sb = new StringBuilder();
|
|
47 |
int c;
|
|
48 |
while ((c = r.read()) > 0)
|
|
49 |
if (c != '\r')
|
|
50 |
sb.append((char) c);
|
|
51 |
return sb.toString();
|
|
52 |
}
|
|
53 |
|
|
54 |
private static String commandOutput(Process p) throws Throwable {
|
|
55 |
check(p.getInputStream() == p.getInputStream());
|
|
56 |
check(p.getOutputStream() == p.getOutputStream());
|
|
57 |
check(p.getErrorStream() == p.getErrorStream());
|
|
58 |
Reader r = new InputStreamReader(p.getInputStream(),"UTF-8");
|
|
59 |
String output = commandOutput(r);
|
|
60 |
equal(p.waitFor(), 0);
|
|
61 |
equal(p.exitValue(), 0);
|
|
62 |
return output;
|
|
63 |
}
|
|
64 |
|
|
65 |
private static String commandOutput(ProcessBuilder pb) {
|
|
66 |
try {
|
|
67 |
return commandOutput(pb.start());
|
|
68 |
} catch (Throwable t) {
|
|
69 |
String commandline = "";
|
|
70 |
for (String arg : pb.command())
|
|
71 |
commandline += " " + arg;
|
|
72 |
System.out.println("Exception trying to run process: " + commandline);
|
|
73 |
unexpected(t);
|
|
74 |
return "";
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
private static String commandOutput(String...command) {
|
|
79 |
try {
|
|
80 |
return commandOutput(Runtime.getRuntime().exec(command));
|
|
81 |
} catch (Throwable t) {
|
|
82 |
String commandline = "";
|
|
83 |
for (String arg : command)
|
|
84 |
commandline += " " + arg;
|
|
85 |
System.out.println("Exception trying to run process: " + commandline);
|
|
86 |
unexpected(t);
|
|
87 |
return "";
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
private static void checkCommandOutput(ProcessBuilder pb,
|
|
92 |
String expected,
|
|
93 |
String failureMsg) {
|
|
94 |
String got = commandOutput(pb);
|
|
95 |
check(got.equals(expected),
|
|
96 |
failureMsg + "\n" +
|
|
97 |
"Expected: \"" + expected + "\"\n" +
|
|
98 |
"Got: \"" + got + "\"");
|
|
99 |
}
|
|
100 |
|
|
101 |
private static String absolutifyPath(String path) {
|
|
102 |
StringBuilder sb = new StringBuilder();
|
|
103 |
for (String file : path.split(File.pathSeparator)) {
|
|
104 |
if (sb.length() != 0)
|
|
105 |
sb.append(File.pathSeparator);
|
|
106 |
sb.append(new File(file).getAbsolutePath());
|
|
107 |
}
|
|
108 |
return sb.toString();
|
|
109 |
}
|
|
110 |
|
|
111 |
// compare windows-style, by canonicalizing to upper case,
|
|
112 |
// not lower case as String.compareToIgnoreCase does
|
|
113 |
private static class WindowsComparator
|
|
114 |
implements Comparator<String> {
|
|
115 |
public int compare(String x, String y) {
|
|
116 |
return x.toUpperCase(Locale.US)
|
|
117 |
.compareTo(y.toUpperCase(Locale.US));
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
private static String sortedLines(String lines) {
|
|
122 |
String[] arr = lines.split("\n");
|
|
123 |
List<String> ls = new ArrayList<String>();
|
|
124 |
for (String s : arr)
|
|
125 |
ls.add(s);
|
|
126 |
Collections.sort(ls, new WindowsComparator());
|
|
127 |
StringBuilder sb = new StringBuilder();
|
|
128 |
for (String s : ls)
|
|
129 |
sb.append(s + "\n");
|
|
130 |
return sb.toString();
|
|
131 |
}
|
|
132 |
|
|
133 |
private static void compareLinesIgnoreCase(String lines1, String lines2) {
|
|
134 |
if (! (sortedLines(lines1).equalsIgnoreCase(sortedLines(lines2)))) {
|
|
135 |
String dashes =
|
|
136 |
"-----------------------------------------------------";
|
|
137 |
out.println(dashes);
|
|
138 |
out.print(sortedLines(lines1));
|
|
139 |
out.println(dashes);
|
|
140 |
out.print(sortedLines(lines2));
|
|
141 |
out.println(dashes);
|
|
142 |
out.println("sizes: " + sortedLines(lines1).length() +
|
|
143 |
" " + sortedLines(lines2).length());
|
|
144 |
|
|
145 |
fail("Sorted string contents differ");
|
|
146 |
}
|
|
147 |
}
|
|
148 |
|
|
149 |
private static final Runtime runtime = Runtime.getRuntime();
|
|
150 |
|
|
151 |
private static final String[] winEnvCommand = {"cmd.exe", "/c", "set"};
|
|
152 |
|
|
153 |
private static String winEnvFilter(String env) {
|
|
154 |
return env.replaceAll("\r", "")
|
|
155 |
.replaceAll("(?m)^(?:COMSPEC|PROMPT|PATHEXT)=.*\n","");
|
|
156 |
}
|
|
157 |
|
|
158 |
private static String unixEnvProg() {
|
|
159 |
return new File("/usr/bin/env").canExecute() ? "/usr/bin/env"
|
|
160 |
: "/bin/env";
|
|
161 |
}
|
|
162 |
|
|
163 |
private static String nativeEnv(String[] env) {
|
|
164 |
try {
|
|
165 |
if (Windows.is()) {
|
|
166 |
return winEnvFilter
|
|
167 |
(commandOutput(runtime.exec(winEnvCommand, env)));
|
|
168 |
} else {
|
|
169 |
return commandOutput(runtime.exec(unixEnvProg(), env));
|
|
170 |
}
|
|
171 |
} catch (Throwable t) { throw new Error(t); }
|
|
172 |
}
|
|
173 |
|
|
174 |
private static String nativeEnv(ProcessBuilder pb) {
|
|
175 |
try {
|
|
176 |
if (Windows.is()) {
|
|
177 |
pb.command(winEnvCommand);
|
|
178 |
return winEnvFilter(commandOutput(pb));
|
|
179 |
} else {
|
|
180 |
pb.command(new String[]{unixEnvProg()});
|
|
181 |
return commandOutput(pb);
|
|
182 |
}
|
|
183 |
} catch (Throwable t) { throw new Error(t); }
|
|
184 |
}
|
|
185 |
|
|
186 |
private static void checkSizes(Map<String,String> environ, int size) {
|
|
187 |
try {
|
|
188 |
equal(size, environ.size());
|
|
189 |
equal(size, environ.entrySet().size());
|
|
190 |
equal(size, environ.keySet().size());
|
|
191 |
equal(size, environ.values().size());
|
|
192 |
|
|
193 |
boolean isEmpty = (size == 0);
|
|
194 |
equal(isEmpty, environ.isEmpty());
|
|
195 |
equal(isEmpty, environ.entrySet().isEmpty());
|
|
196 |
equal(isEmpty, environ.keySet().isEmpty());
|
|
197 |
equal(isEmpty, environ.values().isEmpty());
|
|
198 |
} catch (Throwable t) { unexpected(t); }
|
|
199 |
}
|
|
200 |
|
|
201 |
private interface EnvironmentFrobber {
|
|
202 |
void doIt(Map<String,String> environ);
|
|
203 |
}
|
|
204 |
|
|
205 |
private static void testVariableDeleter(EnvironmentFrobber fooDeleter) {
|
|
206 |
try {
|
|
207 |
Map<String,String> environ = new ProcessBuilder().environment();
|
|
208 |
environ.put("Foo", "BAAR");
|
|
209 |
fooDeleter.doIt(environ);
|
|
210 |
equal(environ.get("Foo"), null);
|
|
211 |
equal(environ.remove("Foo"), null);
|
|
212 |
} catch (Throwable t) { unexpected(t); }
|
|
213 |
}
|
|
214 |
|
|
215 |
private static void testVariableAdder(EnvironmentFrobber fooAdder) {
|
|
216 |
try {
|
|
217 |
Map<String,String> environ = new ProcessBuilder().environment();
|
|
218 |
environ.remove("Foo");
|
|
219 |
fooAdder.doIt(environ);
|
|
220 |
equal(environ.get("Foo"), "Bahrein");
|
|
221 |
} catch (Throwable t) { unexpected(t); }
|
|
222 |
}
|
|
223 |
|
|
224 |
private static void testVariableModifier(EnvironmentFrobber fooModifier) {
|
|
225 |
try {
|
|
226 |
Map<String,String> environ = new ProcessBuilder().environment();
|
|
227 |
environ.put("Foo","OldValue");
|
|
228 |
fooModifier.doIt(environ);
|
|
229 |
equal(environ.get("Foo"), "NewValue");
|
|
230 |
} catch (Throwable t) { unexpected(t); }
|
|
231 |
}
|
|
232 |
|
|
233 |
private static void printUTF8(String s) throws IOException {
|
|
234 |
out.write(s.getBytes("UTF-8"));
|
|
235 |
}
|
|
236 |
|
|
237 |
private static String getenvAsString(Map<String,String> environment) {
|
|
238 |
StringBuilder sb = new StringBuilder();
|
|
239 |
for (Map.Entry<String,String> e : environment.entrySet())
|
|
240 |
// Ignore magic environment variables added by the launcher
|
|
241 |
if (! e.getKey().equals("NLSPATH") &&
|
|
242 |
! e.getKey().equals("XFILESEARCHPATH") &&
|
|
243 |
! e.getKey().equals("LD_LIBRARY_PATH"))
|
|
244 |
sb.append(e.getKey())
|
|
245 |
.append('=')
|
|
246 |
.append(e.getValue())
|
|
247 |
.append(',');
|
|
248 |
return sb.toString();
|
|
249 |
}
|
|
250 |
|
|
251 |
static void print4095(OutputStream s) throws Throwable {
|
|
252 |
byte[] bytes = new byte[4095];
|
|
253 |
Arrays.fill(bytes, (byte) '!');
|
|
254 |
s.write(bytes); // Might hang!
|
|
255 |
}
|
|
256 |
|
|
257 |
public static class JavaChild {
|
|
258 |
public static void main(String args[]) throws Throwable {
|
|
259 |
String action = args[0];
|
|
260 |
if (action.equals("System.getenv(String)")) {
|
|
261 |
String val = System.getenv(args[1]);
|
|
262 |
printUTF8(val == null ? "null" : val);
|
|
263 |
} else if (action.equals("System.getenv(\\u1234)")) {
|
|
264 |
String val = System.getenv("\u1234");
|
|
265 |
printUTF8(val == null ? "null" : val);
|
|
266 |
} else if (action.equals("System.getenv()")) {
|
|
267 |
printUTF8(getenvAsString(System.getenv()));
|
|
268 |
} else if (action.equals("pwd")) {
|
|
269 |
printUTF8(new File(System.getProperty("user.dir"))
|
|
270 |
.getCanonicalPath());
|
|
271 |
} else if (action.equals("print4095")) {
|
|
272 |
print4095(System.out);
|
|
273 |
System.exit(5);
|
|
274 |
} else if (action.equals("OutErr")) {
|
|
275 |
// You might think the system streams would be
|
|
276 |
// buffered, and in fact they are implemented using
|
|
277 |
// BufferedOutputStream, but each and every print
|
|
278 |
// causes immediate operating system I/O.
|
|
279 |
System.out.print("out");
|
|
280 |
System.err.print("err");
|
|
281 |
System.out.print("out");
|
|
282 |
System.err.print("err");
|
|
283 |
} else if (action.equals("null PATH")) {
|
|
284 |
equal(System.getenv("PATH"), null);
|
|
285 |
check(new File("/bin/true").exists());
|
|
286 |
check(new File("/bin/false").exists());
|
|
287 |
ProcessBuilder pb1 = new ProcessBuilder();
|
|
288 |
ProcessBuilder pb2 = new ProcessBuilder();
|
|
289 |
pb2.environment().put("PATH", "anyOldPathIgnoredAnyways");
|
|
290 |
ProcessResults r;
|
|
291 |
|
|
292 |
for (final ProcessBuilder pb :
|
|
293 |
new ProcessBuilder[] {pb1, pb2}) {
|
|
294 |
pb.command("true");
|
|
295 |
r = run(pb.start());
|
|
296 |
equal(r.exitValue(), True.exitValue());
|
|
297 |
|
|
298 |
pb.command("false");
|
|
299 |
r = run(pb.start());
|
|
300 |
equal(r.exitValue(), False.exitValue());
|
|
301 |
}
|
|
302 |
|
|
303 |
if (failed != 0) throw new Error("null PATH");
|
|
304 |
} else if (action.equals("PATH search algorithm")) {
|
|
305 |
equal(System.getenv("PATH"), "dir1:dir2:");
|
|
306 |
check(new File("/bin/true").exists());
|
|
307 |
check(new File("/bin/false").exists());
|
|
308 |
String[] cmd = {"prog"};
|
|
309 |
ProcessBuilder pb1 = new ProcessBuilder(cmd);
|
|
310 |
ProcessBuilder pb2 = new ProcessBuilder(cmd);
|
|
311 |
ProcessBuilder pb3 = new ProcessBuilder(cmd);
|
|
312 |
pb2.environment().put("PATH", "anyOldPathIgnoredAnyways");
|
|
313 |
pb3.environment().remove("PATH");
|
|
314 |
|
|
315 |
for (final ProcessBuilder pb :
|
|
316 |
new ProcessBuilder[] {pb1, pb2, pb3}) {
|
|
317 |
try {
|
|
318 |
// Not on PATH at all; directories don't exist
|
|
319 |
try {
|
|
320 |
pb.start();
|
|
321 |
fail("Expected IOException not thrown");
|
|
322 |
} catch (IOException e) {
|
|
323 |
String m = e.getMessage();
|
|
324 |
if (EnglishUnix.is() &&
|
|
325 |
! matches(m, "No such file"))
|
|
326 |
unexpected(e);
|
|
327 |
} catch (Throwable t) { unexpected(t); }
|
|
328 |
|
|
329 |
// Not on PATH at all; directories exist
|
|
330 |
new File("dir1").mkdirs();
|
|
331 |
new File("dir2").mkdirs();
|
|
332 |
try {
|
|
333 |
pb.start();
|
|
334 |
fail("Expected IOException not thrown");
|
|
335 |
} catch (IOException e) {
|
|
336 |
String m = e.getMessage();
|
|
337 |
if (EnglishUnix.is() &&
|
|
338 |
! matches(m, "No such file"))
|
|
339 |
unexpected(e);
|
|
340 |
} catch (Throwable t) { unexpected(t); }
|
|
341 |
|
|
342 |
// Can't execute a directory -- permission denied
|
|
343 |
// Report EACCES errno
|
|
344 |
new File("dir1/prog").mkdirs();
|
|
345 |
try {
|
|
346 |
pb.start();
|
|
347 |
fail("Expected IOException not thrown");
|
|
348 |
} catch (IOException e) {
|
|
349 |
String m = e.getMessage();
|
|
350 |
if (EnglishUnix.is() &&
|
|
351 |
! matches(m, "Permission denied"))
|
|
352 |
unexpected(e);
|
|
353 |
} catch (Throwable t) { unexpected(t); }
|
|
354 |
|
|
355 |
// continue searching if EACCES
|
|
356 |
copy("/bin/true", "dir2/prog");
|
|
357 |
equal(run(pb.start()).exitValue(), True.exitValue());
|
|
358 |
new File("dir1/prog").delete();
|
|
359 |
new File("dir2/prog").delete();
|
|
360 |
|
|
361 |
new File("dir2/prog").mkdirs();
|
|
362 |
copy("/bin/true", "dir1/prog");
|
|
363 |
equal(run(pb.start()).exitValue(), True.exitValue());
|
|
364 |
|
|
365 |
// Check empty PATH component means current directory
|
|
366 |
new File("dir1/prog").delete();
|
|
367 |
new File("dir2/prog").delete();
|
|
368 |
copy("/bin/true", "./prog");
|
|
369 |
equal(run(pb.start()).exitValue(), True.exitValue());
|
|
370 |
|
|
371 |
// If prog found on both parent and child's PATH,
|
|
372 |
// parent's is used.
|
|
373 |
new File("dir1/prog").delete();
|
|
374 |
new File("dir2/prog").delete();
|
|
375 |
new File("prog").delete();
|
|
376 |
new File("dir3").mkdirs();
|
|
377 |
copy("/bin/true", "dir1/prog");
|
|
378 |
copy("/bin/false", "dir3/prog");
|
|
379 |
pb.environment().put("PATH","dir3");
|
|
380 |
equal(run(pb.start()).exitValue(), True.exitValue());
|
|
381 |
copy("/bin/true", "dir3/prog");
|
|
382 |
copy("/bin/false", "dir1/prog");
|
|
383 |
equal(run(pb.start()).exitValue(), False.exitValue());
|
|
384 |
|
|
385 |
} finally {
|
|
386 |
// cleanup
|
|
387 |
new File("dir1/prog").delete();
|
|
388 |
new File("dir2/prog").delete();
|
|
389 |
new File("dir3/prog").delete();
|
|
390 |
new File("dir1").delete();
|
|
391 |
new File("dir2").delete();
|
|
392 |
new File("dir3").delete();
|
|
393 |
new File("prog").delete();
|
|
394 |
}
|
|
395 |
}
|
|
396 |
|
|
397 |
if (failed != 0) throw new Error("PATH search algorithm");
|
|
398 |
}
|
|
399 |
else throw new Error("JavaChild invocation error");
|
|
400 |
}
|
|
401 |
}
|
|
402 |
|
|
403 |
private static void copy(String src, String dst) {
|
|
404 |
system("/bin/cp", "-fp", src, dst);
|
|
405 |
}
|
|
406 |
|
|
407 |
private static void system(String... command) {
|
|
408 |
try {
|
|
409 |
ProcessBuilder pb = new ProcessBuilder(command);
|
|
410 |
ProcessResults r = run(pb.start());
|
|
411 |
equal(r.exitValue(), 0);
|
|
412 |
equal(r.out(), "");
|
|
413 |
equal(r.err(), "");
|
|
414 |
} catch (Throwable t) { unexpected(t); }
|
|
415 |
}
|
|
416 |
|
|
417 |
private static String javaChildOutput(ProcessBuilder pb, String...args) {
|
|
418 |
List<String> list = new ArrayList<String>(javaChildArgs);
|
|
419 |
for (String arg : args)
|
|
420 |
list.add(arg);
|
|
421 |
pb.command(list);
|
|
422 |
return commandOutput(pb);
|
|
423 |
}
|
|
424 |
|
|
425 |
private static String getenvInChild(ProcessBuilder pb) {
|
|
426 |
return javaChildOutput(pb, "System.getenv()");
|
|
427 |
}
|
|
428 |
|
|
429 |
private static String getenvInChild1234(ProcessBuilder pb) {
|
|
430 |
return javaChildOutput(pb, "System.getenv(\\u1234)");
|
|
431 |
}
|
|
432 |
|
|
433 |
private static String getenvInChild(ProcessBuilder pb, String name) {
|
|
434 |
return javaChildOutput(pb, "System.getenv(String)", name);
|
|
435 |
}
|
|
436 |
|
|
437 |
private static String pwdInChild(ProcessBuilder pb) {
|
|
438 |
return javaChildOutput(pb, "pwd");
|
|
439 |
}
|
|
440 |
|
|
441 |
private static final String javaExe =
|
|
442 |
System.getProperty("java.home") +
|
|
443 |
File.separator + "bin" + File.separator + "java";
|
|
444 |
|
|
445 |
private static final String classpath =
|
|
446 |
System.getProperty("java.class.path");
|
|
447 |
|
|
448 |
private static final List<String> javaChildArgs =
|
|
449 |
Arrays.asList(new String[]
|
|
450 |
{ javaExe, "-classpath", absolutifyPath(classpath),
|
|
451 |
"Basic$JavaChild"});
|
|
452 |
|
|
453 |
private static void testEncoding(String encoding, String tested) {
|
|
454 |
try {
|
|
455 |
// If round trip conversion works, should be able to set env vars
|
|
456 |
// correctly in child.
|
|
457 |
if (new String(tested.getBytes()).equals(tested)) {
|
|
458 |
out.println("Testing " + encoding + " environment values");
|
|
459 |
ProcessBuilder pb = new ProcessBuilder();
|
|
460 |
pb.environment().put("ASCIINAME",tested);
|
|
461 |
equal(getenvInChild(pb,"ASCIINAME"), tested);
|
|
462 |
}
|
|
463 |
} catch (Throwable t) { unexpected(t); }
|
|
464 |
}
|
|
465 |
|
|
466 |
static class Windows {
|
|
467 |
public static boolean is() { return is; }
|
|
468 |
private static final boolean is =
|
|
469 |
System.getProperty("os.name").startsWith("Windows");
|
|
470 |
}
|
|
471 |
|
|
472 |
static class Unix {
|
|
473 |
public static boolean is() { return is; }
|
|
474 |
private static final boolean is =
|
|
475 |
(! Windows.is() &&
|
|
476 |
new File("/bin/sh").exists() &&
|
|
477 |
new File("/bin/true").exists() &&
|
|
478 |
new File("/bin/false").exists());
|
|
479 |
}
|
|
480 |
|
|
481 |
static class UnicodeOS {
|
|
482 |
public static boolean is() { return is; }
|
|
483 |
private static final String osName = System.getProperty("os.name");
|
|
484 |
private static final boolean is =
|
|
485 |
// MacOS X would probably also qualify
|
|
486 |
osName.startsWith("Windows") &&
|
|
487 |
! osName.startsWith("Windows 9") &&
|
|
488 |
! osName.equals("Windows Me");
|
|
489 |
}
|
|
490 |
|
|
491 |
static class True {
|
|
492 |
public static int exitValue() { return 0; }
|
|
493 |
}
|
|
494 |
|
|
495 |
private static class False {
|
|
496 |
public static int exitValue() { return exitValue; }
|
|
497 |
private static final int exitValue = exitValue0();
|
|
498 |
private static int exitValue0() {
|
|
499 |
// /bin/false returns an *unspecified* non-zero number.
|
|
500 |
try {
|
|
501 |
if (! Unix.is())
|
|
502 |
return -1;
|
|
503 |
else {
|
|
504 |
int rc = new ProcessBuilder("/bin/false")
|
|
505 |
.start().waitFor();
|
|
506 |
check(rc != 0);
|
|
507 |
return rc;
|
|
508 |
}
|
|
509 |
} catch (Throwable t) { unexpected(t); return -1; }
|
|
510 |
}
|
|
511 |
}
|
|
512 |
|
|
513 |
static class EnglishUnix {
|
|
514 |
private final static Boolean is =
|
|
515 |
(! Windows.is() && isEnglish("LANG") && isEnglish("LC_ALL"));
|
|
516 |
|
|
517 |
private static boolean isEnglish(String envvar) {
|
|
518 |
String val = getenv(envvar);
|
|
519 |
return (val == null) || val.matches("en.*");
|
|
520 |
}
|
|
521 |
|
|
522 |
/** Returns true if we can expect English OS error strings */
|
|
523 |
static boolean is() { return is; }
|
|
524 |
}
|
|
525 |
|
|
526 |
private static boolean matches(String str, String regex) {
|
|
527 |
return Pattern.compile(regex).matcher(str).find();
|
|
528 |
}
|
|
529 |
|
|
530 |
private static String sortByLinesWindowsly(String text) {
|
|
531 |
String[] lines = text.split("\n");
|
|
532 |
Arrays.sort(lines, new WindowsComparator());
|
|
533 |
StringBuilder sb = new StringBuilder();
|
|
534 |
for (String line : lines)
|
|
535 |
sb.append(line).append("\n");
|
|
536 |
return sb.toString();
|
|
537 |
}
|
|
538 |
|
|
539 |
private static void checkMapSanity(Map<String,String> map) {
|
|
540 |
try {
|
|
541 |
Set<String> keySet = map.keySet();
|
|
542 |
Collection<String> values = map.values();
|
|
543 |
Set<Map.Entry<String,String>> entrySet = map.entrySet();
|
|
544 |
|
|
545 |
equal(entrySet.size(), keySet.size());
|
|
546 |
equal(entrySet.size(), values.size());
|
|
547 |
|
|
548 |
StringBuilder s1 = new StringBuilder();
|
|
549 |
for (Map.Entry<String,String> e : entrySet)
|
|
550 |
s1.append(e.getKey() + "=" + e.getValue() + "\n");
|
|
551 |
|
|
552 |
StringBuilder s2 = new StringBuilder();
|
|
553 |
for (String var : keySet)
|
|
554 |
s2.append(var + "=" + map.get(var) + "\n");
|
|
555 |
|
|
556 |
equal(s1.toString(), s2.toString());
|
|
557 |
|
|
558 |
Iterator<String> kIter = keySet.iterator();
|
|
559 |
Iterator<String> vIter = values.iterator();
|
|
560 |
Iterator<Map.Entry<String,String>> eIter = entrySet.iterator();
|
|
561 |
|
|
562 |
while (eIter.hasNext()) {
|
|
563 |
Map.Entry<String,String> entry = eIter.next();
|
|
564 |
String key = kIter.next();
|
|
565 |
String value = vIter.next();
|
|
566 |
check(entrySet.contains(entry));
|
|
567 |
check(keySet.contains(key));
|
|
568 |
check(values.contains(value));
|
|
569 |
check(map.containsKey(key));
|
|
570 |
check(map.containsValue(value));
|
|
571 |
equal(entry.getKey(), key);
|
|
572 |
equal(entry.getValue(), value);
|
|
573 |
}
|
|
574 |
check(! kIter.hasNext() &&
|
|
575 |
! vIter.hasNext());
|
|
576 |
|
|
577 |
} catch (Throwable t) { unexpected(t); }
|
|
578 |
}
|
|
579 |
|
|
580 |
private static void checkMapEquality(Map<String,String> map1,
|
|
581 |
Map<String,String> map2) {
|
|
582 |
try {
|
|
583 |
equal(map1.size(), map2.size());
|
|
584 |
equal(map1.isEmpty(), map2.isEmpty());
|
|
585 |
for (String key : map1.keySet()) {
|
|
586 |
equal(map1.get(key), map2.get(key));
|
|
587 |
check(map2.keySet().contains(key));
|
|
588 |
}
|
|
589 |
equal(map1, map2);
|
|
590 |
equal(map2, map1);
|
|
591 |
equal(map1.entrySet(), map2.entrySet());
|
|
592 |
equal(map2.entrySet(), map1.entrySet());
|
|
593 |
equal(map1.keySet(), map2.keySet());
|
|
594 |
equal(map2.keySet(), map1.keySet());
|
|
595 |
|
|
596 |
equal(map1.hashCode(), map2.hashCode());
|
|
597 |
equal(map1.entrySet().hashCode(), map2.entrySet().hashCode());
|
|
598 |
equal(map1.keySet().hashCode(), map2.keySet().hashCode());
|
|
599 |
} catch (Throwable t) { unexpected(t); }
|
|
600 |
}
|
|
601 |
|
|
602 |
private static void realMain(String[] args) throws Throwable {
|
|
603 |
if (Windows.is())
|
|
604 |
System.out.println("This appears to be a Windows system.");
|
|
605 |
if (Unix.is())
|
|
606 |
System.out.println("This appears to be a Unix system.");
|
|
607 |
if (UnicodeOS.is())
|
|
608 |
System.out.println("This appears to be a Unicode-based OS.");
|
|
609 |
|
|
610 |
//----------------------------------------------------------------
|
|
611 |
// Basic tests for setting, replacing and deleting envvars
|
|
612 |
//----------------------------------------------------------------
|
|
613 |
try {
|
|
614 |
ProcessBuilder pb = new ProcessBuilder();
|
|
615 |
Map<String,String> environ = pb.environment();
|
|
616 |
|
|
617 |
// New env var
|
|
618 |
environ.put("QUUX", "BAR");
|
|
619 |
equal(environ.get("QUUX"), "BAR");
|
|
620 |
equal(getenvInChild(pb,"QUUX"), "BAR");
|
|
621 |
|
|
622 |
// Modify env var
|
|
623 |
environ.put("QUUX","bear");
|
|
624 |
equal(environ.get("QUUX"), "bear");
|
|
625 |
equal(getenvInChild(pb,"QUUX"), "bear");
|
|
626 |
checkMapSanity(environ);
|
|
627 |
|
|
628 |
// Remove env var
|
|
629 |
environ.remove("QUUX");
|
|
630 |
equal(environ.get("QUUX"), null);
|
|
631 |
equal(getenvInChild(pb,"QUUX"), "null");
|
|
632 |
checkMapSanity(environ);
|
|
633 |
|
|
634 |
// Remove non-existent env var
|
|
635 |
environ.remove("QUUX");
|
|
636 |
equal(environ.get("QUUX"), null);
|
|
637 |
equal(getenvInChild(pb,"QUUX"), "null");
|
|
638 |
checkMapSanity(environ);
|
|
639 |
} catch (Throwable t) { unexpected(t); }
|
|
640 |
|
|
641 |
//----------------------------------------------------------------
|
|
642 |
// Pass Empty environment to child
|
|
643 |
//----------------------------------------------------------------
|
|
644 |
try {
|
|
645 |
ProcessBuilder pb = new ProcessBuilder();
|
|
646 |
pb.environment().clear();
|
|
647 |
equal(getenvInChild(pb), "");
|
|
648 |
} catch (Throwable t) { unexpected(t); }
|
|
649 |
|
|
650 |
//----------------------------------------------------------------
|
|
651 |
// System.getenv() is read-only.
|
|
652 |
//----------------------------------------------------------------
|
|
653 |
THROWS(UnsupportedOperationException.class,
|
|
654 |
new Fun(){void f(){ getenv().put("FOO","BAR");}},
|
|
655 |
new Fun(){void f(){ getenv().remove("PATH");}},
|
|
656 |
new Fun(){void f(){ getenv().keySet().remove("PATH");}},
|
|
657 |
new Fun(){void f(){ getenv().values().remove("someValue");}});
|
|
658 |
|
|
659 |
try {
|
|
660 |
Collection<Map.Entry<String,String>> c = getenv().entrySet();
|
|
661 |
if (! c.isEmpty())
|
|
662 |
try {
|
|
663 |
c.iterator().next().setValue("foo");
|
|
664 |
fail("Expected UnsupportedOperationException not thrown");
|
|
665 |
} catch (UnsupportedOperationException e) {} // OK
|
|
666 |
} catch (Throwable t) { unexpected(t); }
|
|
667 |
|
|
668 |
//----------------------------------------------------------------
|
|
669 |
// System.getenv() always returns the same object in our implementation.
|
|
670 |
//----------------------------------------------------------------
|
|
671 |
try {
|
|
672 |
check(System.getenv() == System.getenv());
|
|
673 |
} catch (Throwable t) { unexpected(t); }
|
|
674 |
|
|
675 |
//----------------------------------------------------------------
|
|
676 |
// You can't create an env var name containing "=",
|
|
677 |
// or an env var name or value containing NUL.
|
|
678 |
//----------------------------------------------------------------
|
|
679 |
{
|
|
680 |
final Map<String,String> m = new ProcessBuilder().environment();
|
|
681 |
THROWS(IllegalArgumentException.class,
|
|
682 |
new Fun(){void f(){ m.put("FOO=","BAR");}},
|
|
683 |
new Fun(){void f(){ m.put("FOO\u0000","BAR");}},
|
|
684 |
new Fun(){void f(){ m.put("FOO","BAR\u0000");}});
|
|
685 |
}
|
|
686 |
|
|
687 |
//----------------------------------------------------------------
|
|
688 |
// Commands must never be null.
|
|
689 |
//----------------------------------------------------------------
|
|
690 |
THROWS(NullPointerException.class,
|
|
691 |
new Fun(){void f(){
|
|
692 |
new ProcessBuilder((List<String>)null);}},
|
|
693 |
new Fun(){void f(){
|
|
694 |
new ProcessBuilder().command((List<String>)null);}});
|
|
695 |
|
|
696 |
//----------------------------------------------------------------
|
|
697 |
// Put in a command; get the same one back out.
|
|
698 |
//----------------------------------------------------------------
|
|
699 |
try {
|
|
700 |
List<String> command = new ArrayList<String>();
|
|
701 |
ProcessBuilder pb = new ProcessBuilder(command);
|
|
702 |
check(pb.command() == command);
|
|
703 |
List<String> command2 = new ArrayList<String>(2);
|
|
704 |
command2.add("foo");
|
|
705 |
command2.add("bar");
|
|
706 |
pb.command(command2);
|
|
707 |
check(pb.command() == command2);
|
|
708 |
pb.command("foo", "bar");
|
|
709 |
check(pb.command() != command2 && pb.command().equals(command2));
|
|
710 |
pb.command(command2);
|
|
711 |
command2.add("baz");
|
|
712 |
equal(pb.command().get(2), "baz");
|
|
713 |
} catch (Throwable t) { unexpected(t); }
|
|
714 |
|
|
715 |
//----------------------------------------------------------------
|
|
716 |
// Commands must contain at least one element.
|
|
717 |
//----------------------------------------------------------------
|
|
718 |
THROWS(IndexOutOfBoundsException.class,
|
|
719 |
new Fun() { void f() throws IOException {
|
|
720 |
new ProcessBuilder().start();}},
|
|
721 |
new Fun() { void f() throws IOException {
|
|
722 |
new ProcessBuilder(new ArrayList<String>()).start();}},
|
|
723 |
new Fun() { void f() throws IOException {
|
|
724 |
Runtime.getRuntime().exec(new String[]{});}});
|
|
725 |
|
|
726 |
//----------------------------------------------------------------
|
|
727 |
// Commands must not contain null elements at start() time.
|
|
728 |
//----------------------------------------------------------------
|
|
729 |
THROWS(NullPointerException.class,
|
|
730 |
new Fun() { void f() throws IOException {
|
|
731 |
new ProcessBuilder("foo",null,"bar").start();}},
|
|
732 |
new Fun() { void f() throws IOException {
|
|
733 |
new ProcessBuilder((String)null).start();}},
|
|
734 |
new Fun() { void f() throws IOException {
|
|
735 |
new ProcessBuilder(new String[]{null}).start();}},
|
|
736 |
new Fun() { void f() throws IOException {
|
|
737 |
new ProcessBuilder(new String[]{"foo",null,"bar"}).start();}});
|
|
738 |
|
|
739 |
//----------------------------------------------------------------
|
|
740 |
// Command lists are growable.
|
|
741 |
//----------------------------------------------------------------
|
|
742 |
try {
|
|
743 |
new ProcessBuilder().command().add("foo");
|
|
744 |
new ProcessBuilder("bar").command().add("foo");
|
|
745 |
new ProcessBuilder(new String[]{"1","2"}).command().add("3");
|
|
746 |
} catch (Throwable t) { unexpected(t); }
|
|
747 |
|
|
748 |
//----------------------------------------------------------------
|
|
749 |
// Nulls in environment updates generate NullPointerException
|
|
750 |
//----------------------------------------------------------------
|
|
751 |
try {
|
|
752 |
final Map<String,String> env = new ProcessBuilder().environment();
|
|
753 |
THROWS(NullPointerException.class,
|
|
754 |
new Fun(){void f(){ env.put("foo",null);}},
|
|
755 |
new Fun(){void f(){ env.put(null,"foo");}},
|
|
756 |
new Fun(){void f(){ env.remove(null);}},
|
|
757 |
new Fun(){void f(){
|
|
758 |
for (Map.Entry<String,String> e : env.entrySet())
|
|
759 |
e.setValue(null);}},
|
|
760 |
new Fun() { void f() throws IOException {
|
|
761 |
Runtime.getRuntime().exec(new String[]{"foo"},
|
|
762 |
new String[]{null});}});
|
|
763 |
} catch (Throwable t) { unexpected(t); }
|
|
764 |
|
|
765 |
//----------------------------------------------------------------
|
|
766 |
// Non-String types in environment updates generate ClassCastException
|
|
767 |
//----------------------------------------------------------------
|
|
768 |
try {
|
|
769 |
final Map<String,String> env = new ProcessBuilder().environment();
|
|
770 |
THROWS(ClassCastException.class,
|
|
771 |
new Fun(){void f(){ env.remove(TRUE);}},
|
|
772 |
new Fun(){void f(){ env.keySet().remove(TRUE);}},
|
|
773 |
new Fun(){void f(){ env.values().remove(TRUE);}},
|
|
774 |
new Fun(){void f(){ env.entrySet().remove(TRUE);}});
|
|
775 |
} catch (Throwable t) { unexpected(t); }
|
|
776 |
|
|
777 |
//----------------------------------------------------------------
|
|
778 |
// Check query operations on environment maps
|
|
779 |
//----------------------------------------------------------------
|
|
780 |
try {
|
|
781 |
List<Map<String,String>> envs =
|
|
782 |
new ArrayList<Map<String,String>>(2);
|
|
783 |
envs.add(System.getenv());
|
|
784 |
envs.add(new ProcessBuilder().environment());
|
|
785 |
for (final Map<String,String> env : envs) {
|
|
786 |
//----------------------------------------------------------------
|
|
787 |
// Nulls in environment queries are forbidden.
|
|
788 |
//----------------------------------------------------------------
|
|
789 |
THROWS(NullPointerException.class,
|
|
790 |
new Fun(){void f(){ getenv(null);}},
|
|
791 |
new Fun(){void f(){ env.get(null);}},
|
|
792 |
new Fun(){void f(){ env.containsKey(null);}},
|
|
793 |
new Fun(){void f(){ env.containsValue(null);}},
|
|
794 |
new Fun(){void f(){ env.keySet().contains(null);}},
|
|
795 |
new Fun(){void f(){ env.values().contains(null);}});
|
|
796 |
|
|
797 |
//----------------------------------------------------------------
|
|
798 |
// Non-String types in environment queries are forbidden.
|
|
799 |
//----------------------------------------------------------------
|
|
800 |
THROWS(ClassCastException.class,
|
|
801 |
new Fun(){void f(){ env.get(TRUE);}},
|
|
802 |
new Fun(){void f(){ env.containsKey(TRUE);}},
|
|
803 |
new Fun(){void f(){ env.containsValue(TRUE);}},
|
|
804 |
new Fun(){void f(){ env.keySet().contains(TRUE);}},
|
|
805 |
new Fun(){void f(){ env.values().contains(TRUE);}});
|
|
806 |
|
|
807 |
//----------------------------------------------------------------
|
|
808 |
// Illegal String values in environment queries are (grumble) OK
|
|
809 |
//----------------------------------------------------------------
|
|
810 |
equal(env.get("\u0000"), null);
|
|
811 |
check(! env.containsKey("\u0000"));
|
|
812 |
check(! env.containsValue("\u0000"));
|
|
813 |
check(! env.keySet().contains("\u0000"));
|
|
814 |
check(! env.values().contains("\u0000"));
|
|
815 |
}
|
|
816 |
|
|
817 |
} catch (Throwable t) { unexpected(t); }
|
|
818 |
|
|
819 |
try {
|
|
820 |
final Set<Map.Entry<String,String>> entrySet =
|
|
821 |
new ProcessBuilder().environment().entrySet();
|
|
822 |
THROWS(NullPointerException.class,
|
|
823 |
new Fun(){void f(){ entrySet.contains(null);}});
|
|
824 |
THROWS(ClassCastException.class,
|
|
825 |
new Fun(){void f(){ entrySet.contains(TRUE);}},
|
|
826 |
new Fun(){void f(){
|
|
827 |
entrySet.contains(
|
|
828 |
new SimpleImmutableEntry<Boolean,String>(TRUE,""));}});
|
|
829 |
|
|
830 |
check(! entrySet.contains
|
|
831 |
(new SimpleImmutableEntry<String,String>("", "")));
|
|
832 |
} catch (Throwable t) { unexpected(t); }
|
|
833 |
|
|
834 |
//----------------------------------------------------------------
|
|
835 |
// Put in a directory; get the same one back out.
|
|
836 |
//----------------------------------------------------------------
|
|
837 |
try {
|
|
838 |
ProcessBuilder pb = new ProcessBuilder();
|
|
839 |
File foo = new File("foo");
|
|
840 |
equal(pb.directory(), null);
|
|
841 |
equal(pb.directory(foo).directory(), foo);
|
|
842 |
equal(pb.directory(null).directory(), null);
|
|
843 |
} catch (Throwable t) { unexpected(t); }
|
|
844 |
|
|
845 |
//----------------------------------------------------------------
|
|
846 |
// If round-trip conversion works, check envvar pass-through to child
|
|
847 |
//----------------------------------------------------------------
|
|
848 |
try {
|
|
849 |
testEncoding("ASCII", "xyzzy");
|
|
850 |
testEncoding("Latin1", "\u00f1\u00e1");
|
|
851 |
testEncoding("Unicode", "\u22f1\u11e1");
|
|
852 |
} catch (Throwable t) { unexpected(t); }
|
|
853 |
|
|
854 |
//----------------------------------------------------------------
|
|
855 |
// A surprisingly large number of ways to delete an environment var.
|
|
856 |
//----------------------------------------------------------------
|
|
857 |
testVariableDeleter(new EnvironmentFrobber() {
|
|
858 |
public void doIt(Map<String,String> environ) {
|
|
859 |
environ.remove("Foo");}});
|
|
860 |
|
|
861 |
testVariableDeleter(new EnvironmentFrobber() {
|
|
862 |
public void doIt(Map<String,String> environ) {
|
|
863 |
environ.keySet().remove("Foo");}});
|
|
864 |
|
|
865 |
testVariableDeleter(new EnvironmentFrobber() {
|
|
866 |
public void doIt(Map<String,String> environ) {
|
|
867 |
environ.values().remove("BAAR");}});
|
|
868 |
|
|
869 |
testVariableDeleter(new EnvironmentFrobber() {
|
|
870 |
public void doIt(Map<String,String> environ) {
|
|
871 |
// Legally fabricate a ProcessEnvironment.StringEntry,
|
|
872 |
// even though it's private.
|
|
873 |
Map<String,String> environ2
|
|
874 |
= new ProcessBuilder().environment();
|
|
875 |
environ2.clear();
|
|
876 |
environ2.put("Foo","BAAR");
|
|
877 |
// Subtlety alert.
|
|
878 |
Map.Entry<String,String> e
|
|
879 |
= environ2.entrySet().iterator().next();
|
|
880 |
environ.entrySet().remove(e);}});
|
|
881 |
|
|
882 |
testVariableDeleter(new EnvironmentFrobber() {
|
|
883 |
public void doIt(Map<String,String> environ) {
|
|
884 |
Map.Entry<String,String> victim = null;
|
|
885 |
for (Map.Entry<String,String> e : environ.entrySet())
|
|
886 |
if (e.getKey().equals("Foo"))
|
|
887 |
victim = e;
|
|
888 |
if (victim != null)
|
|
889 |
environ.entrySet().remove(victim);}});
|
|
890 |
|
|
891 |
testVariableDeleter(new EnvironmentFrobber() {
|
|
892 |
public void doIt(Map<String,String> environ) {
|
|
893 |
Iterator<String> it = environ.keySet().iterator();
|
|
894 |
while (it.hasNext()) {
|
|
895 |
String val = it.next();
|
|
896 |
if (val.equals("Foo"))
|
|
897 |
it.remove();}}});
|
|
898 |
|
|
899 |
testVariableDeleter(new EnvironmentFrobber() {
|
|
900 |
public void doIt(Map<String,String> environ) {
|
|
901 |
Iterator<Map.Entry<String,String>> it
|
|
902 |
= environ.entrySet().iterator();
|
|
903 |
while (it.hasNext()) {
|
|
904 |
Map.Entry<String,String> e = it.next();
|
|
905 |
if (e.getKey().equals("Foo"))
|
|
906 |
it.remove();}}});
|
|
907 |
|
|
908 |
testVariableDeleter(new EnvironmentFrobber() {
|
|
909 |
public void doIt(Map<String,String> environ) {
|
|
910 |
Iterator<String> it = environ.values().iterator();
|
|
911 |
while (it.hasNext()) {
|
|
912 |
String val = it.next();
|
|
913 |
if (val.equals("BAAR"))
|
|
914 |
it.remove();}}});
|
|
915 |
|
|
916 |
//----------------------------------------------------------------
|
|
917 |
// A surprisingly small number of ways to add an environment var.
|
|
918 |
//----------------------------------------------------------------
|
|
919 |
testVariableAdder(new EnvironmentFrobber() {
|
|
920 |
public void doIt(Map<String,String> environ) {
|
|
921 |
environ.put("Foo","Bahrein");}});
|
|
922 |
|
|
923 |
//----------------------------------------------------------------
|
|
924 |
// A few ways to modify an environment var.
|
|
925 |
//----------------------------------------------------------------
|
|
926 |
testVariableModifier(new EnvironmentFrobber() {
|
|
927 |
public void doIt(Map<String,String> environ) {
|
|
928 |
environ.put("Foo","NewValue");}});
|
|
929 |
|
|
930 |
testVariableModifier(new EnvironmentFrobber() {
|
|
931 |
public void doIt(Map<String,String> environ) {
|
|
932 |
for (Map.Entry<String,String> e : environ.entrySet())
|
|
933 |
if (e.getKey().equals("Foo"))
|
|
934 |
e.setValue("NewValue");}});
|
|
935 |
|
|
936 |
//----------------------------------------------------------------
|
|
937 |
// Fiddle with environment sizes
|
|
938 |
//----------------------------------------------------------------
|
|
939 |
try {
|
|
940 |
Map<String,String> environ = new ProcessBuilder().environment();
|
|
941 |
int size = environ.size();
|
|
942 |
checkSizes(environ, size);
|
|
943 |
|
|
944 |
environ.put("UnLiKeLYeNVIROmtNam", "someVal");
|
|
945 |
checkSizes(environ, size+1);
|
|
946 |
|
|
947 |
// Check for environment independence
|
|
948 |
new ProcessBuilder().environment().clear();
|
|
949 |
|
|
950 |
environ.put("UnLiKeLYeNVIROmtNam", "someOtherVal");
|
|
951 |
checkSizes(environ, size+1);
|
|
952 |
|
|
953 |
environ.remove("UnLiKeLYeNVIROmtNam");
|
|
954 |
checkSizes(environ, size);
|
|
955 |
|
|
956 |
environ.clear();
|
|
957 |
checkSizes(environ, 0);
|
|
958 |
|
|
959 |
environ.clear();
|
|
960 |
checkSizes(environ, 0);
|
|
961 |
|
|
962 |
environ = new ProcessBuilder().environment();
|
|
963 |
environ.keySet().clear();
|
|
964 |
checkSizes(environ, 0);
|
|
965 |
|
|
966 |
environ = new ProcessBuilder().environment();
|
|
967 |
environ.entrySet().clear();
|
|
968 |
checkSizes(environ, 0);
|
|
969 |
|
|
970 |
environ = new ProcessBuilder().environment();
|
|
971 |
environ.values().clear();
|
|
972 |
checkSizes(environ, 0);
|
|
973 |
} catch (Throwable t) { unexpected(t); }
|
|
974 |
|
|
975 |
//----------------------------------------------------------------
|
|
976 |
// Check that various map invariants hold
|
|
977 |
//----------------------------------------------------------------
|
|
978 |
checkMapSanity(new ProcessBuilder().environment());
|
|
979 |
checkMapSanity(System.getenv());
|
|
980 |
checkMapEquality(new ProcessBuilder().environment(),
|
|
981 |
new ProcessBuilder().environment());
|
|
982 |
|
|
983 |
|
|
984 |
//----------------------------------------------------------------
|
|
985 |
// Check effects on external "env" command.
|
|
986 |
//----------------------------------------------------------------
|
|
987 |
try {
|
|
988 |
Set<String> env1 = new HashSet<String>
|
|
989 |
(Arrays.asList(nativeEnv((String[])null).split("\n")));
|
|
990 |
|
|
991 |
ProcessBuilder pb = new ProcessBuilder();
|
|
992 |
pb.environment().put("QwErTyUiOp","AsDfGhJk");
|
|
993 |
|
|
994 |
Set<String> env2 = new HashSet<String>
|
|
995 |
(Arrays.asList(nativeEnv(pb).split("\n")));
|
|
996 |
|
|
997 |
check(env2.size() == env1.size() + 1);
|
|
998 |
env1.add("QwErTyUiOp=AsDfGhJk");
|
|
999 |
check(env1.equals(env2));
|
|
1000 |
} catch (Throwable t) { unexpected(t); }
|
|
1001 |
|
|
1002 |
//----------------------------------------------------------------
|
|
1003 |
// Test Runtime.exec(...envp...)
|
|
1004 |
// Check for sort order of environment variables on Windows.
|
|
1005 |
//----------------------------------------------------------------
|
|
1006 |
try {
|
|
1007 |
// '+' < 'A' < 'Z' < '_' < 'a' < 'z' < '~'
|
|
1008 |
String[]envp = {"FOO=BAR","BAZ=GORP","QUUX=",
|
|
1009 |
"+=+", "_=_", "~=~"};
|
|
1010 |
String output = nativeEnv(envp);
|
|
1011 |
String expected = "+=+\nBAZ=GORP\nFOO=BAR\nQUUX=\n_=_\n~=~\n";
|
|
1012 |
// On Windows, Java must keep the environment sorted.
|
|
1013 |
// Order is random on Unix, so this test does the sort.
|
|
1014 |
if (! Windows.is())
|
|
1015 |
output = sortByLinesWindowsly(output);
|
|
1016 |
equal(output, expected);
|
|
1017 |
} catch (Throwable t) { unexpected(t); }
|
|
1018 |
|
|
1019 |
//----------------------------------------------------------------
|
|
1020 |
// System.getenv() must be consistent with System.getenv(String)
|
|
1021 |
//----------------------------------------------------------------
|
|
1022 |
try {
|
|
1023 |
for (Map.Entry<String,String> e : getenv().entrySet())
|
|
1024 |
equal(getenv(e.getKey()), e.getValue());
|
|
1025 |
} catch (Throwable t) { unexpected(t); }
|
|
1026 |
|
|
1027 |
//----------------------------------------------------------------
|
|
1028 |
// Fiddle with working directory in child
|
|
1029 |
//----------------------------------------------------------------
|
|
1030 |
try {
|
|
1031 |
String canonicalUserDir =
|
|
1032 |
new File(System.getProperty("user.dir")).getCanonicalPath();
|
|
1033 |
String[] sdirs = new String[]
|
|
1034 |
{".", "..", "/", "/bin",
|
|
1035 |
"C:", "c:", "C:/", "c:\\", "\\", "\\bin" };
|
|
1036 |
for (String sdir : sdirs) {
|
|
1037 |
File dir = new File(sdir);
|
|
1038 |
if (! (dir.isDirectory() && dir.exists()))
|
|
1039 |
continue;
|
|
1040 |
out.println("Testing directory " + dir);
|
|
1041 |
dir = new File(dir.getCanonicalPath());
|
|
1042 |
|
|
1043 |
ProcessBuilder pb = new ProcessBuilder();
|
|
1044 |
equal(pb.directory(), null);
|
|
1045 |
equal(pwdInChild(pb), canonicalUserDir);
|
|
1046 |
|
|
1047 |
pb.directory(dir);
|
|
1048 |
equal(pb.directory(), dir);
|
|
1049 |
equal(pwdInChild(pb), dir.toString());
|
|
1050 |
|
|
1051 |
pb.directory(null);
|
|
1052 |
equal(pb.directory(), null);
|
|
1053 |
equal(pwdInChild(pb), canonicalUserDir);
|
|
1054 |
|
|
1055 |
pb.directory(dir);
|
|
1056 |
}
|
|
1057 |
} catch (Throwable t) { unexpected(t); }
|
|
1058 |
|
|
1059 |
//----------------------------------------------------------------
|
|
1060 |
// Windows has tricky semi-case-insensitive semantics
|
|
1061 |
//----------------------------------------------------------------
|
|
1062 |
if (Windows.is())
|
|
1063 |
try {
|
|
1064 |
out.println("Running case insensitve variable tests");
|
|
1065 |
for (String[] namePair :
|
|
1066 |
new String[][]
|
|
1067 |
{ new String[]{"PATH","PaTh"},
|
|
1068 |
new String[]{"home","HOME"},
|
|
1069 |
new String[]{"SYSTEMROOT","SystemRoot"}}) {
|
|
1070 |
check((getenv(namePair[0]) == null &&
|
|
1071 |
getenv(namePair[1]) == null)
|
|
1072 |
||
|
|
1073 |
getenv(namePair[0]).equals(getenv(namePair[1])),
|
|
1074 |
"Windows environment variables are not case insensitive");
|
|
1075 |
}
|
|
1076 |
} catch (Throwable t) { unexpected(t); }
|
|
1077 |
|
|
1078 |
//----------------------------------------------------------------
|
|
1079 |
// Test proper Unicode child environment transfer
|
|
1080 |
//----------------------------------------------------------------
|
|
1081 |
if (UnicodeOS.is())
|
|
1082 |
try {
|
|
1083 |
ProcessBuilder pb = new ProcessBuilder();
|
|
1084 |
pb.environment().put("\u1234","\u5678");
|
|
1085 |
pb.environment().remove("PATH");
|
|
1086 |
equal(getenvInChild1234(pb), "\u5678");
|
|
1087 |
} catch (Throwable t) { unexpected(t); }
|
|
1088 |
|
|
1089 |
|
|
1090 |
//----------------------------------------------------------------
|
|
1091 |
// Test Runtime.exec(...envp...) with envstrings with initial `='
|
|
1092 |
//----------------------------------------------------------------
|
|
1093 |
try {
|
|
1094 |
List<String> childArgs = new ArrayList<String>(javaChildArgs);
|
|
1095 |
childArgs.add("System.getenv()");
|
|
1096 |
String[] cmdp = childArgs.toArray(new String[childArgs.size()]);
|
|
1097 |
String[] envp = {"=ExitValue=3", "=C:=\\"};
|
|
1098 |
Process p = Runtime.getRuntime().exec(cmdp, envp);
|
|
1099 |
String expected = Windows.is() ? "=C:=\\,=ExitValue=3," : "=C:=\\,";
|
|
1100 |
equal(commandOutput(p), expected);
|
|
1101 |
if (Windows.is()) {
|
|
1102 |
ProcessBuilder pb = new ProcessBuilder(childArgs);
|
|
1103 |
pb.environment().clear();
|
|
1104 |
pb.environment().put("=ExitValue", "3");
|
|
1105 |
pb.environment().put("=C:", "\\");
|
|
1106 |
equal(commandOutput(pb), expected);
|
|
1107 |
}
|
|
1108 |
} catch (Throwable t) { unexpected(t); }
|
|
1109 |
|
|
1110 |
//----------------------------------------------------------------
|
|
1111 |
// Test Runtime.exec(...envp...) with envstrings without any `='
|
|
1112 |
//----------------------------------------------------------------
|
|
1113 |
try {
|
|
1114 |
String[] cmdp = {"echo"};
|
|
1115 |
String[] envp = {"Hello", "World"}; // Yuck!
|
|
1116 |
Process p = Runtime.getRuntime().exec(cmdp, envp);
|
|
1117 |
equal(commandOutput(p), "\n");
|
|
1118 |
} catch (Throwable t) { unexpected(t); }
|
|
1119 |
|
|
1120 |
//----------------------------------------------------------------
|
|
1121 |
// Test Runtime.exec(...envp...) with envstrings containing NULs
|
|
1122 |
//----------------------------------------------------------------
|
|
1123 |
try {
|
|
1124 |
List<String> childArgs = new ArrayList<String>(javaChildArgs);
|
|
1125 |
childArgs.add("System.getenv()");
|
|
1126 |
String[] cmdp = childArgs.toArray(new String[childArgs.size()]);
|
|
1127 |
String[] envp = {"LC_ALL=C\u0000\u0000", // Yuck!
|
|
1128 |
"FO\u0000=B\u0000R"};
|
|
1129 |
Process p = Runtime.getRuntime().exec(cmdp, envp);
|
|
1130 |
check(commandOutput(p).equals("LC_ALL=C,"),
|
|
1131 |
"Incorrect handling of envstrings containing NULs");
|
|
1132 |
} catch (Throwable t) { unexpected(t); }
|
|
1133 |
|
|
1134 |
//----------------------------------------------------------------
|
|
1135 |
// Test the redirectErrorStream property
|
|
1136 |
//----------------------------------------------------------------
|
|
1137 |
try {
|
|
1138 |
ProcessBuilder pb = new ProcessBuilder();
|
|
1139 |
equal(pb.redirectErrorStream(), false);
|
|
1140 |
equal(pb.redirectErrorStream(true), pb);
|
|
1141 |
equal(pb.redirectErrorStream(), true);
|
|
1142 |
equal(pb.redirectErrorStream(false), pb);
|
|
1143 |
equal(pb.redirectErrorStream(), false);
|
|
1144 |
} catch (Throwable t) { unexpected(t); }
|
|
1145 |
|
|
1146 |
try {
|
|
1147 |
List<String> childArgs = new ArrayList<String>(javaChildArgs);
|
|
1148 |
childArgs.add("OutErr");
|
|
1149 |
ProcessBuilder pb = new ProcessBuilder(childArgs);
|
|
1150 |
{
|
|
1151 |
ProcessResults r = run(pb.start());
|
|
1152 |
equal(r.out(), "outout");
|
|
1153 |
equal(r.err(), "errerr");
|
|
1154 |
}
|
|
1155 |
{
|
|
1156 |
pb.redirectErrorStream(true);
|
|
1157 |
ProcessResults r = run(pb.start());
|
|
1158 |
equal(r.out(), "outerrouterr");
|
|
1159 |
equal(r.err(), "");
|
|
1160 |
}
|
|
1161 |
} catch (Throwable t) { unexpected(t); }
|
|
1162 |
|
|
1163 |
if (! Windows.is() &&
|
|
1164 |
new File("/bin/true").exists() &&
|
|
1165 |
new File("/bin/false").exists()) {
|
|
1166 |
//----------------------------------------------------------------
|
|
1167 |
// We can find true and false when PATH is null
|
|
1168 |
//----------------------------------------------------------------
|
|
1169 |
try {
|
|
1170 |
List<String> childArgs = new ArrayList<String>(javaChildArgs);
|
|
1171 |
childArgs.add("null PATH");
|
|
1172 |
ProcessBuilder pb = new ProcessBuilder(childArgs);
|
|
1173 |
pb.environment().remove("PATH");
|
|
1174 |
ProcessResults r = run(pb.start());
|
|
1175 |
equal(r.out(), "");
|
|
1176 |
equal(r.err(), "");
|
|
1177 |
equal(r.exitValue(), 0);
|
|
1178 |
} catch (Throwable t) { unexpected(t); }
|
|
1179 |
|
|
1180 |
//----------------------------------------------------------------
|
|
1181 |
// PATH search algorithm on Unix
|
|
1182 |
//----------------------------------------------------------------
|
|
1183 |
try {
|
|
1184 |
List<String> childArgs = new ArrayList<String>(javaChildArgs);
|
|
1185 |
childArgs.add("PATH search algorithm");
|
|
1186 |
ProcessBuilder pb = new ProcessBuilder(childArgs);
|
|
1187 |
pb.environment().put("PATH", "dir1:dir2:");
|
|
1188 |
ProcessResults r = run(pb.start());
|
|
1189 |
equal(r.out(), "");
|
|
1190 |
equal(r.err(), "");
|
|
1191 |
equal(r.exitValue(), True.exitValue());
|
|
1192 |
} catch (Throwable t) { unexpected(t); }
|
|
1193 |
|
|
1194 |
//----------------------------------------------------------------
|
|
1195 |
// Parent's, not child's PATH is used
|
|
1196 |
//----------------------------------------------------------------
|
|
1197 |
try {
|
|
1198 |
new File("suBdiR").mkdirs();
|
|
1199 |
copy("/bin/true", "suBdiR/unliKely");
|
|
1200 |
final ProcessBuilder pb =
|
|
1201 |
new ProcessBuilder(new String[]{"unliKely"});
|
|
1202 |
pb.environment().put("PATH", "suBdiR");
|
|
1203 |
THROWS(IOException.class,
|
|
1204 |
new Fun() {void f() throws Throwable {pb.start();}});
|
|
1205 |
} catch (Throwable t) { unexpected(t);
|
|
1206 |
} finally {
|
|
1207 |
new File("suBdiR/unliKely").delete();
|
|
1208 |
new File("suBdiR").delete();
|
|
1209 |
}
|
|
1210 |
}
|
|
1211 |
|
|
1212 |
//----------------------------------------------------------------
|
|
1213 |
// Attempt to start bogus program ""
|
|
1214 |
//----------------------------------------------------------------
|
|
1215 |
try {
|
|
1216 |
new ProcessBuilder("").start();
|
|
1217 |
fail("Expected IOException not thrown");
|
|
1218 |
} catch (IOException e) {
|
|
1219 |
String m = e.getMessage();
|
|
1220 |
if (EnglishUnix.is() &&
|
|
1221 |
! matches(m, "No such file or directory"))
|
|
1222 |
unexpected(e);
|
|
1223 |
} catch (Throwable t) { unexpected(t); }
|
|
1224 |
|
|
1225 |
//----------------------------------------------------------------
|
|
1226 |
// Check that attempt to execute program name with funny
|
|
1227 |
// characters throws an exception containing those characters.
|
|
1228 |
//----------------------------------------------------------------
|
|
1229 |
for (String programName : new String[] {"\u00f0", "\u01f0"})
|
|
1230 |
try {
|
|
1231 |
new ProcessBuilder(programName).start();
|
|
1232 |
fail("Expected IOException not thrown");
|
|
1233 |
} catch (IOException e) {
|
|
1234 |
String m = e.getMessage();
|
|
1235 |
Pattern p = Pattern.compile(programName);
|
|
1236 |
if (! matches(m, programName)
|
|
1237 |
|| (EnglishUnix.is()
|
|
1238 |
&& ! matches(m, "No such file or directory")))
|
|
1239 |
unexpected(e);
|
|
1240 |
} catch (Throwable t) { unexpected(t); }
|
|
1241 |
|
|
1242 |
//----------------------------------------------------------------
|
|
1243 |
// Attempt to start process in nonexistent directory fails.
|
|
1244 |
//----------------------------------------------------------------
|
|
1245 |
try {
|
|
1246 |
new ProcessBuilder("echo")
|
|
1247 |
.directory(new File("UnLiKeLY"))
|
|
1248 |
.start();
|
|
1249 |
fail("Expected IOException not thrown");
|
|
1250 |
} catch (IOException e) {
|
|
1251 |
String m = e.getMessage();
|
|
1252 |
if (! matches(m, "in directory")
|
|
1253 |
|| (EnglishUnix.is() &&
|
|
1254 |
! matches(m, "No such file or directory")))
|
|
1255 |
unexpected(e);
|
|
1256 |
} catch (Throwable t) { unexpected(t); }
|
|
1257 |
|
|
1258 |
//----------------------------------------------------------------
|
|
1259 |
// This would deadlock, if not for the fact that
|
|
1260 |
// interprocess pipe buffers are at least 4096 bytes.
|
|
1261 |
//----------------------------------------------------------------
|
|
1262 |
try {
|
|
1263 |
List<String> childArgs = new ArrayList<String>(javaChildArgs);
|
|
1264 |
childArgs.add("print4095");
|
|
1265 |
Process p = new ProcessBuilder(childArgs).start();
|
|
1266 |
print4095(p.getOutputStream()); // Might hang!
|
|
1267 |
p.waitFor(); // Might hang!
|
|
1268 |
equal(p.exitValue(), 5);
|
|
1269 |
} catch (Throwable t) { unexpected(t); }
|
|
1270 |
|
|
1271 |
//----------------------------------------------------------------
|
|
1272 |
// Attempt to start process with insufficient permissions fails.
|
|
1273 |
//----------------------------------------------------------------
|
|
1274 |
try {
|
|
1275 |
new File("emptyCommand").delete();
|
|
1276 |
new FileOutputStream("emptyCommand").close();
|
|
1277 |
new File("emptyCommand").setExecutable(false);
|
|
1278 |
new ProcessBuilder("./emptyCommand").start();
|
|
1279 |
fail("Expected IOException not thrown");
|
|
1280 |
} catch (IOException e) {
|
|
1281 |
new File("./emptyCommand").delete();
|
|
1282 |
String m = e.getMessage();
|
|
1283 |
//e.printStackTrace();
|
|
1284 |
if (EnglishUnix.is() &&
|
|
1285 |
! matches(m, "Permission denied"))
|
|
1286 |
unexpected(e);
|
|
1287 |
} catch (Throwable t) { unexpected(t); }
|
|
1288 |
|
|
1289 |
new File("emptyCommand").delete();
|
|
1290 |
|
|
1291 |
//----------------------------------------------------------------
|
|
1292 |
// Check for correct security permission behavior
|
|
1293 |
//----------------------------------------------------------------
|
|
1294 |
final Policy policy = new Policy();
|
|
1295 |
Policy.setPolicy(policy);
|
|
1296 |
System.setSecurityManager(new SecurityManager());
|
|
1297 |
|
|
1298 |
try {
|
|
1299 |
// No permissions required to CREATE a ProcessBuilder
|
|
1300 |
policy.setPermissions(/* Nothing */);
|
|
1301 |
new ProcessBuilder("env").directory(null).directory();
|
|
1302 |
new ProcessBuilder("env").directory(new File("dir")).directory();
|
|
1303 |
new ProcessBuilder("env").command("??").command();
|
|
1304 |
} catch (Throwable t) { unexpected(t); }
|
|
1305 |
|
|
1306 |
THROWS(SecurityException.class,
|
|
1307 |
new Fun() { void f() throws IOException {
|
|
1308 |
policy.setPermissions(/* Nothing */);
|
|
1309 |
System.getenv("foo");}},
|
|
1310 |
new Fun() { void f() throws IOException {
|
|
1311 |
policy.setPermissions(/* Nothing */);
|
|
1312 |
System.getenv();}},
|
|
1313 |
new Fun() { void f() throws IOException {
|
|
1314 |
policy.setPermissions(/* Nothing */);
|
|
1315 |
new ProcessBuilder("echo").start();}},
|
|
1316 |
new Fun() { void f() throws IOException {
|
|
1317 |
policy.setPermissions(/* Nothing */);
|
|
1318 |
Runtime.getRuntime().exec("echo");}},
|
|
1319 |
new Fun() { void f() throws IOException {
|
|
1320 |
policy.setPermissions(new RuntimePermission("getenv.bar"));
|
|
1321 |
System.getenv("foo");}});
|
|
1322 |
|
|
1323 |
try {
|
|
1324 |
policy.setPermissions(new RuntimePermission("getenv.foo"));
|
|
1325 |
System.getenv("foo");
|
|
1326 |
|
|
1327 |
policy.setPermissions(new RuntimePermission("getenv.*"));
|
|
1328 |
System.getenv("foo");
|
|
1329 |
System.getenv();
|
|
1330 |
new ProcessBuilder().environment();
|
|
1331 |
} catch (Throwable t) { unexpected(t); }
|
|
1332 |
|
|
1333 |
|
|
1334 |
final Permission execPermission
|
|
1335 |
= new FilePermission("<<ALL FILES>>", "execute");
|
|
1336 |
|
|
1337 |
THROWS(SecurityException.class,
|
|
1338 |
new Fun() { void f() throws IOException {
|
|
1339 |
// environment permission by itself insufficient
|
|
1340 |
policy.setPermissions(new RuntimePermission("getenv.*"));
|
|
1341 |
ProcessBuilder pb = new ProcessBuilder("env");
|
|
1342 |
pb.environment().put("foo","bar");
|
|
1343 |
pb.start();}},
|
|
1344 |
new Fun() { void f() throws IOException {
|
|
1345 |
// exec permission by itself insufficient
|
|
1346 |
policy.setPermissions(execPermission);
|
|
1347 |
ProcessBuilder pb = new ProcessBuilder("env");
|
|
1348 |
pb.environment().put("foo","bar");
|
|
1349 |
pb.start();}});
|
|
1350 |
|
|
1351 |
try {
|
|
1352 |
// Both permissions? OK.
|
|
1353 |
policy.setPermissions(new RuntimePermission("getenv.*"),
|
|
1354 |
execPermission);
|
|
1355 |
ProcessBuilder pb = new ProcessBuilder("env");
|
|
1356 |
pb.environment().put("foo","bar");
|
|
1357 |
pb.start();
|
|
1358 |
} catch (IOException e) { // OK
|
|
1359 |
} catch (Throwable t) { unexpected(t); }
|
|
1360 |
|
|
1361 |
try {
|
|
1362 |
// Don't need environment permission unless READING environment
|
|
1363 |
policy.setPermissions(execPermission);
|
|
1364 |
Runtime.getRuntime().exec("env", new String[]{});
|
|
1365 |
} catch (IOException e) { // OK
|
|
1366 |
} catch (Throwable t) { unexpected(t); }
|
|
1367 |
|
|
1368 |
try {
|
|
1369 |
// Don't need environment permission unless READING environment
|
|
1370 |
policy.setPermissions(execPermission);
|
|
1371 |
new ProcessBuilder("env").start();
|
|
1372 |
} catch (IOException e) { // OK
|
|
1373 |
} catch (Throwable t) { unexpected(t); }
|
|
1374 |
|
|
1375 |
// Restore "normal" state without a security manager
|
|
1376 |
policy.setPermissions(new RuntimePermission("setSecurityManager"));
|
|
1377 |
System.setSecurityManager(null);
|
|
1378 |
|
|
1379 |
}
|
|
1380 |
|
|
1381 |
//----------------------------------------------------------------
|
|
1382 |
// A Policy class designed to make permissions fiddling very easy.
|
|
1383 |
//----------------------------------------------------------------
|
|
1384 |
private static class Policy extends java.security.Policy {
|
|
1385 |
private Permissions perms;
|
|
1386 |
|
|
1387 |
public void setPermissions(Permission...permissions) {
|
|
1388 |
perms = new Permissions();
|
|
1389 |
for (Permission permission : permissions)
|
|
1390 |
perms.add(permission);
|
|
1391 |
}
|
|
1392 |
|
|
1393 |
public Policy() { setPermissions(/* Nothing */); }
|
|
1394 |
|
|
1395 |
public PermissionCollection getPermissions(CodeSource cs) {
|
|
1396 |
return perms;
|
|
1397 |
}
|
|
1398 |
|
|
1399 |
public PermissionCollection getPermissions(ProtectionDomain pd) {
|
|
1400 |
return perms;
|
|
1401 |
}
|
|
1402 |
|
|
1403 |
public boolean implies(ProtectionDomain pd, Permission p) {
|
|
1404 |
return perms.implies(p);
|
|
1405 |
}
|
|
1406 |
|
|
1407 |
public void refresh() {}
|
|
1408 |
}
|
|
1409 |
|
|
1410 |
private static class StreamAccumulator extends Thread {
|
|
1411 |
private final InputStream is;
|
|
1412 |
private final StringBuilder sb = new StringBuilder();
|
|
1413 |
private Throwable throwable = null;
|
|
1414 |
|
|
1415 |
public String result () throws Throwable {
|
|
1416 |
if (throwable != null)
|
|
1417 |
throw throwable;
|
|
1418 |
return sb.toString();
|
|
1419 |
}
|
|
1420 |
|
|
1421 |
StreamAccumulator (InputStream is) {
|
|
1422 |
this.is = is;
|
|
1423 |
}
|
|
1424 |
|
|
1425 |
public void run() {
|
|
1426 |
try {
|
|
1427 |
Reader r = new InputStreamReader(is);
|
|
1428 |
char[] buf = new char[4096];
|
|
1429 |
int n;
|
|
1430 |
while ((n = r.read(buf)) > 0) {
|
|
1431 |
sb.append(buf,0,n);
|
|
1432 |
}
|
|
1433 |
} catch (Throwable t) {
|
|
1434 |
throwable = t;
|
|
1435 |
}
|
|
1436 |
}
|
|
1437 |
}
|
|
1438 |
|
|
1439 |
private static ProcessResults run(Process p) {
|
|
1440 |
Throwable throwable = null;
|
|
1441 |
int exitValue = -1;
|
|
1442 |
String out = "";
|
|
1443 |
String err = "";
|
|
1444 |
|
|
1445 |
StreamAccumulator outAccumulator =
|
|
1446 |
new StreamAccumulator(p.getInputStream());
|
|
1447 |
StreamAccumulator errAccumulator =
|
|
1448 |
new StreamAccumulator(p.getErrorStream());
|
|
1449 |
|
|
1450 |
try {
|
|
1451 |
outAccumulator.start();
|
|
1452 |
errAccumulator.start();
|
|
1453 |
|
|
1454 |
exitValue = p.waitFor();
|
|
1455 |
|
|
1456 |
outAccumulator.join();
|
|
1457 |
errAccumulator.join();
|
|
1458 |
|
|
1459 |
out = outAccumulator.result();
|
|
1460 |
err = errAccumulator.result();
|
|
1461 |
} catch (Throwable t) {
|
|
1462 |
throwable = t;
|
|
1463 |
}
|
|
1464 |
|
|
1465 |
return new ProcessResults(out, err, exitValue, throwable);
|
|
1466 |
}
|
|
1467 |
|
|
1468 |
//----------------------------------------------------------------
|
|
1469 |
// Results of a command
|
|
1470 |
//----------------------------------------------------------------
|
|
1471 |
private static class ProcessResults {
|
|
1472 |
private final String out;
|
|
1473 |
private final String err;
|
|
1474 |
private final int exitValue;
|
|
1475 |
private final Throwable throwable;
|
|
1476 |
|
|
1477 |
public ProcessResults(String out,
|
|
1478 |
String err,
|
|
1479 |
int exitValue,
|
|
1480 |
Throwable throwable) {
|
|
1481 |
this.out = out;
|
|
1482 |
this.err = err;
|
|
1483 |
this.exitValue = exitValue;
|
|
1484 |
this.throwable = throwable;
|
|
1485 |
}
|
|
1486 |
|
|
1487 |
public String out() { return out; }
|
|
1488 |
public String err() { return err; }
|
|
1489 |
public int exitValue() { return exitValue; }
|
|
1490 |
public Throwable throwable() { return throwable; }
|
|
1491 |
|
|
1492 |
public String toString() {
|
|
1493 |
StringBuilder sb = new StringBuilder();
|
|
1494 |
sb.append("<STDOUT>\n" + out() + "</STDOUT>\n")
|
|
1495 |
.append("<STDERR>\n" + err() + "</STDERR>\n")
|
|
1496 |
.append("exitValue = " + exitValue + "\n");
|
|
1497 |
if (throwable != null)
|
|
1498 |
sb.append(throwable.getStackTrace());
|
|
1499 |
return sb.toString();
|
|
1500 |
}
|
|
1501 |
}
|
|
1502 |
|
|
1503 |
//--------------------- Infrastructure ---------------------------
|
|
1504 |
static volatile int passed = 0, failed = 0;
|
|
1505 |
static void pass() {passed++;}
|
|
1506 |
static void fail() {failed++; Thread.dumpStack();}
|
|
1507 |
static void fail(String msg) {System.out.println(msg); fail();}
|
|
1508 |
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
|
|
1509 |
static void check(boolean cond) {if (cond) pass(); else fail();}
|
|
1510 |
static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
|
|
1511 |
static void equal(Object x, Object y) {
|
|
1512 |
if (x == null ? y == null : x.equals(y)) pass();
|
|
1513 |
else fail(x + " not equal to " + y);}
|
|
1514 |
public static void main(String[] args) throws Throwable {
|
|
1515 |
try {realMain(args);} catch (Throwable t) {unexpected(t);}
|
|
1516 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
|
|
1517 |
if (failed > 0) throw new AssertionError("Some tests failed");}
|
|
1518 |
private static abstract class Fun {abstract void f() throws Throwable;}
|
|
1519 |
static void THROWS(Class<? extends Throwable> k, Fun... fs) {
|
|
1520 |
for (Fun f : fs)
|
|
1521 |
try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
|
|
1522 |
catch (Throwable t) {
|
|
1523 |
if (k.isAssignableFrom(t.getClass())) pass();
|
|
1524 |
else unexpected(t);}}
|
|
1525 |
}
|