-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Basic.java
2833 lines (2529 loc) · 119 KB
/
Basic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4199068 4738465 4937983 4930681 4926230 4931433 4932663 4986689
* 5026830 5023243 5070673 4052517 4811767 6192449 6397034 6413313
* 6464154 6523983 6206031 4960438 6631352 6631966 6850957 6850958
* 4947220 7018606 7034570 4244896 5049299 8003488 8054494 8058464
* 8067796 8224905 8263729
* @key intermittent
* @summary Basic tests for Process and Environment Variable code
* @modules java.base/java.lang:open
* @library /test/lib
* @run main/othervm/timeout=300 Basic
* @run main/othervm/timeout=300 -Djdk.lang.Process.launchMechanism=fork Basic
* @author Martin Buchholz
*/
/*
* @test
* @modules java.base/java.lang:open
* @requires (os.family == "linux")
* @library /test/lib
* @run main/othervm/timeout=300 -Djdk.lang.Process.launchMechanism=posix_spawn Basic
*/
import java.lang.ProcessBuilder.Redirect;
import java.lang.ProcessHandle;
import static java.lang.ProcessBuilder.Redirect.*;
import java.io.*;
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.security.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import static java.lang.System.getenv;
import static java.lang.System.out;
import static java.lang.Boolean.TRUE;
import static java.util.AbstractMap.SimpleImmutableEntry;
import jdk.test.lib.Platform;
public class Basic {
/* used for Windows only */
static final String systemRoot = System.getenv("SystemRoot");
/* used for Mac OS X only */
static final String cfUserTextEncoding = System.getenv("__CF_USER_TEXT_ENCODING");
/* used for AIX only */
static final String libpath = System.getenv("LIBPATH");
/* Used for regex String matching for long error messages */
static final String PERMISSION_DENIED_ERROR_MSG = "(Permission denied|error=13)";
static final String NO_SUCH_FILE_ERROR_MSG = "(No such file|error=2)";
/**
* Returns the number of milliseconds since time given by
* startNanoTime, which must have been previously returned from a
* call to {@link System.nanoTime()}.
*/
private static long millisElapsedSince(long startNanoTime) {
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
}
private static String commandOutput(Reader r) throws Throwable {
StringBuilder sb = new StringBuilder();
int c;
while ((c = r.read()) > 0)
if (c != '\r')
sb.append((char) c);
return sb.toString();
}
private static String commandOutput(Process p) throws Throwable {
check(p.getInputStream() == p.getInputStream());
check(p.getOutputStream() == p.getOutputStream());
check(p.getErrorStream() == p.getErrorStream());
Reader r = new InputStreamReader(p.getInputStream(),"UTF-8");
String output = commandOutput(r);
equal(p.waitFor(), 0);
equal(p.exitValue(), 0);
// The debug/fastdebug versions of the VM may write some warnings to stdout
// (i.e. "Warning: Cannot open log file: hotspot.log" if the VM is started
// in a directory without write permissions). These warnings will confuse tests
// which match the entire output of the child process so better filter them out.
return output.replaceAll("Warning:.*\\n", "");
}
private static String commandOutput(ProcessBuilder pb) {
try {
return commandOutput(pb.start());
} catch (Throwable t) {
String commandline = "";
for (String arg : pb.command())
commandline += " " + arg;
System.out.println("Exception trying to run process: " + commandline);
unexpected(t);
return "";
}
}
private static String commandOutput(String...command) {
try {
return commandOutput(Runtime.getRuntime().exec(command));
} catch (Throwable t) {
String commandline = "";
for (String arg : command)
commandline += " " + arg;
System.out.println("Exception trying to run process: " + commandline);
unexpected(t);
return "";
}
}
private static void checkCommandOutput(ProcessBuilder pb,
String expected,
String failureMsg) {
String got = commandOutput(pb);
check(got.equals(expected),
failureMsg + "\n" +
"Expected: \"" + expected + "\"\n" +
"Got: \"" + got + "\"");
}
private static String absolutifyPath(String path) {
StringBuilder sb = new StringBuilder();
for (String file : path.split(File.pathSeparator)) {
if (sb.length() != 0)
sb.append(File.pathSeparator);
sb.append(new File(file).getAbsolutePath());
}
return sb.toString();
}
// compare windows-style, by canonicalizing to upper case,
// not lower case as String.compareToIgnoreCase does
private static class WindowsComparator
implements Comparator<String> {
public int compare(String x, String y) {
return x.toUpperCase(Locale.US)
.compareTo(y.toUpperCase(Locale.US));
}
}
private static String sortedLines(String lines) {
String[] arr = lines.split("\n");
List<String> ls = new ArrayList<String>();
for (String s : arr)
ls.add(s);
Collections.sort(ls, new WindowsComparator());
StringBuilder sb = new StringBuilder();
for (String s : ls)
sb.append(s + "\n");
return sb.toString();
}
private static void compareLinesIgnoreCase(String lines1, String lines2) {
if (! (sortedLines(lines1).equalsIgnoreCase(sortedLines(lines2)))) {
String dashes =
"-----------------------------------------------------";
out.println(dashes);
out.print(sortedLines(lines1));
out.println(dashes);
out.print(sortedLines(lines2));
out.println(dashes);
out.println("sizes: " + sortedLines(lines1).length() +
" " + sortedLines(lines2).length());
fail("Sorted string contents differ");
}
}
private static final Runtime runtime = Runtime.getRuntime();
private static final String[] winEnvCommand = {"cmd.exe", "/c", "set"};
private static String winEnvFilter(String env) {
return env.replaceAll("\r", "")
.replaceAll("(?m)^(?:COMSPEC|PROMPT|PATHEXT)=.*\n","");
}
private static String unixEnvProg() {
return new File("/usr/bin/env").canExecute() ? "/usr/bin/env"
: "/bin/env";
}
private static String nativeEnv(String[] env) {
try {
if (Windows.is()) {
return winEnvFilter
(commandOutput(runtime.exec(winEnvCommand, env)));
} else {
return commandOutput(runtime.exec(unixEnvProg(), env));
}
} catch (Throwable t) { throw new Error(t); }
}
private static String nativeEnv(ProcessBuilder pb) {
try {
if (Windows.is()) {
pb.command(winEnvCommand);
return winEnvFilter(commandOutput(pb));
} else {
pb.command(new String[]{unixEnvProg()});
return commandOutput(pb);
}
} catch (Throwable t) { throw new Error(t); }
}
private static void checkSizes(Map<String,String> environ, int size) {
try {
equal(size, environ.size());
equal(size, environ.entrySet().size());
equal(size, environ.keySet().size());
equal(size, environ.values().size());
boolean isEmpty = (size == 0);
equal(isEmpty, environ.isEmpty());
equal(isEmpty, environ.entrySet().isEmpty());
equal(isEmpty, environ.keySet().isEmpty());
equal(isEmpty, environ.values().isEmpty());
} catch (Throwable t) { unexpected(t); }
}
private interface EnvironmentFrobber {
void doIt(Map<String,String> environ);
}
private static void testVariableDeleter(EnvironmentFrobber fooDeleter) {
try {
Map<String,String> environ = new ProcessBuilder().environment();
environ.put("Foo", "BAAR");
fooDeleter.doIt(environ);
equal(environ.get("Foo"), null);
equal(environ.remove("Foo"), null);
} catch (Throwable t) { unexpected(t); }
}
private static void testVariableAdder(EnvironmentFrobber fooAdder) {
try {
Map<String,String> environ = new ProcessBuilder().environment();
environ.remove("Foo");
fooAdder.doIt(environ);
equal(environ.get("Foo"), "Bahrein");
} catch (Throwable t) { unexpected(t); }
}
private static void testVariableModifier(EnvironmentFrobber fooModifier) {
try {
Map<String,String> environ = new ProcessBuilder().environment();
environ.put("Foo","OldValue");
fooModifier.doIt(environ);
equal(environ.get("Foo"), "NewValue");
} catch (Throwable t) { unexpected(t); }
}
private static void printUTF8(String s) throws IOException {
out.write(s.getBytes("UTF-8"));
}
private static String getenvAsString(Map<String,String> environment) {
StringBuilder sb = new StringBuilder();
environment = new TreeMap<>(environment);
for (Map.Entry<String,String> e : environment.entrySet())
// Ignore magic environment variables added by the launcher
if (! e.getKey().equals("LD_LIBRARY_PATH"))
sb.append(e.getKey())
.append('=')
.append(e.getValue())
.append(',');
return sb.toString();
}
static void print4095(OutputStream s, byte b) throws Throwable {
byte[] bytes = new byte[4095];
Arrays.fill(bytes, b);
s.write(bytes); // Might hang!
}
static void checkPermissionDenied(ProcessBuilder pb) {
try {
pb.start();
fail("Expected IOException not thrown");
} catch (IOException e) {
String m = e.getMessage();
if (EnglishUnix.is() &&
! matches(m, PERMISSION_DENIED_ERROR_MSG))
unexpected(e);
} catch (Throwable t) { unexpected(t); }
}
public static class JavaChild {
public static void main(String args[]) throws Throwable {
String action = args[0];
if (action.equals("sleep")) {
Thread.sleep(10 * 60 * 1000L);
} else if (action.equals("pid")) {
System.out.println(ProcessHandle.current().pid());
} else if (action.equals("testIO")) {
String expected = "standard input";
char[] buf = new char[expected.length()+1];
int n = new InputStreamReader(System.in).read(buf,0,buf.length);
if (n != expected.length())
System.exit(5);
if (! new String(buf,0,n).equals(expected))
System.exit(5);
System.err.print("standard error");
System.out.print("standard output");
} else if (action.equals("testInheritIO")
|| action.equals("testRedirectInherit")) {
List<String> childArgs = new ArrayList<String>(javaChildArgs);
childArgs.add("testIO");
ProcessBuilder pb = new ProcessBuilder(childArgs);
if (action.equals("testInheritIO"))
pb.inheritIO();
else
redirectIO(pb, INHERIT, INHERIT, INHERIT);
ProcessResults r = run(pb);
if (! r.out().equals(""))
System.exit(7);
if (! r.err().equals(""))
System.exit(8);
if (r.exitValue() != 0)
System.exit(9);
} else if (action.equals("System.getenv(String)")) {
String val = System.getenv(args[1]);
printUTF8(val == null ? "null" : val);
} else if (action.equals("System.getenv(\\u1234)")) {
String val = System.getenv("\u1234");
printUTF8(val == null ? "null" : val);
} else if (action.equals("System.getenv()")) {
printUTF8(getenvAsString(System.getenv()));
} else if (action.equals("ArrayOOME")) {
Object dummy;
switch(new Random().nextInt(3)) {
case 0: dummy = new Integer[Integer.MAX_VALUE]; break;
case 1: dummy = new double[Integer.MAX_VALUE]; break;
case 2: dummy = new byte[Integer.MAX_VALUE][]; break;
default: throw new InternalError();
}
} else if (action.equals("pwd")) {
printUTF8(new File(System.getProperty("user.dir"))
.getCanonicalPath());
} else if (action.equals("print4095")) {
print4095(System.out, (byte) '!');
print4095(System.err, (byte) 'E');
System.exit(5);
} else if (action.equals("OutErr")) {
// You might think the system streams would be
// buffered, and in fact they are implemented using
// BufferedOutputStream, but each and every print
// causes immediate operating system I/O.
System.out.print("out");
System.err.print("err");
System.out.print("out");
System.err.print("err");
} else if (action.equals("null PATH")) {
equal(System.getenv("PATH"), null);
check(new File("/bin/true").exists());
check(new File("/bin/false").exists());
ProcessBuilder pb1 = new ProcessBuilder();
ProcessBuilder pb2 = new ProcessBuilder();
pb2.environment().put("PATH", "anyOldPathIgnoredAnyways");
ProcessResults r;
for (final ProcessBuilder pb :
new ProcessBuilder[] {pb1, pb2}) {
pb.command("true");
equal(run(pb).exitValue(), True.exitValue());
pb.command("false");
equal(run(pb).exitValue(), False.exitValue());
}
if (failed != 0) throw new Error("null PATH");
} else if (action.equals("PATH search algorithm")) {
equal(System.getenv("PATH"), "dir1:dir2:");
check(new File(TrueExe.path()).exists());
check(new File(FalseExe.path()).exists());
String[] cmd = {"prog"};
ProcessBuilder pb1 = new ProcessBuilder(cmd);
ProcessBuilder pb2 = new ProcessBuilder(cmd);
ProcessBuilder pb3 = new ProcessBuilder(cmd);
pb2.environment().put("PATH", "anyOldPathIgnoredAnyways");
pb3.environment().remove("PATH");
for (final ProcessBuilder pb :
new ProcessBuilder[] {pb1, pb2, pb3}) {
try {
// Not on PATH at all; directories don't exist
try {
pb.start();
fail("Expected IOException not thrown");
} catch (IOException e) {
String m = e.getMessage();
if (EnglishUnix.is() &&
! matches(m, NO_SUCH_FILE_ERROR_MSG))
unexpected(e);
} catch (Throwable t) { unexpected(t); }
// Not on PATH at all; directories exist
new File("dir1").mkdirs();
new File("dir2").mkdirs();
try {
pb.start();
fail("Expected IOException not thrown");
} catch (IOException e) {
String m = e.getMessage();
if (EnglishUnix.is() &&
! matches(m, NO_SUCH_FILE_ERROR_MSG))
unexpected(e);
} catch (Throwable t) { unexpected(t); }
// Can't execute a directory -- permission denied
// Report EACCES errno
new File("dir1/prog").mkdirs();
checkPermissionDenied(pb);
// continue searching if EACCES
copy(TrueExe.path(), "dir2/prog");
equal(run(pb).exitValue(), True.exitValue());
new File("dir1/prog").delete();
new File("dir2/prog").delete();
new File("dir2/prog").mkdirs();
copy(TrueExe.path(), "dir1/prog");
equal(run(pb).exitValue(), True.exitValue());
// Check empty PATH component means current directory.
//
// While we're here, let's test different kinds of
// Unix executables, and PATH vs explicit searching.
new File("dir1/prog").delete();
new File("dir2/prog").delete();
for (String[] command :
new String[][] {
new String[] {"./prog"},
cmd}) {
pb.command(command);
File prog = new File("./prog");
// "Normal" binaries
copy(TrueExe.path(), "./prog");
equal(run(pb).exitValue(),
True.exitValue());
copy(FalseExe.path(), "./prog");
equal(run(pb).exitValue(),
False.exitValue());
prog.delete();
// Interpreter scripts with #!
setFileContents(prog, "#!/bin/true\n");
prog.setExecutable(true);
equal(run(pb).exitValue(),
True.exitValue());
prog.delete();
setFileContents(prog, "#!/bin/false\n");
prog.setExecutable(true);
equal(run(pb).exitValue(),
False.exitValue());
// Traditional shell scripts without #!
setFileContents(prog, "exec /bin/true\n");
prog.setExecutable(true);
equal(run(pb).exitValue(),
True.exitValue());
prog.delete();
setFileContents(prog, "exec /bin/false\n");
prog.setExecutable(true);
equal(run(pb).exitValue(),
False.exitValue());
prog.delete();
}
// Test Unix interpreter scripts
File dir1Prog = new File("dir1/prog");
dir1Prog.delete();
pb.command(new String[] {"prog", "world"});
setFileContents(dir1Prog, "#!/bin/echo hello\n");
checkPermissionDenied(pb);
dir1Prog.setExecutable(true);
equal(run(pb).out(), "hello dir1/prog world\n");
equal(run(pb).exitValue(), True.exitValue());
dir1Prog.delete();
pb.command(cmd);
// Test traditional shell scripts without #!
setFileContents(dir1Prog, "/bin/echo \"$@\"\n");
pb.command(new String[] {"prog", "hello", "world"});
checkPermissionDenied(pb);
dir1Prog.setExecutable(true);
equal(run(pb).out(), "hello world\n");
equal(run(pb).exitValue(), True.exitValue());
dir1Prog.delete();
pb.command(cmd);
// If prog found on both parent and child's PATH,
// parent's is used.
new File("dir1/prog").delete();
new File("dir2/prog").delete();
new File("prog").delete();
new File("dir3").mkdirs();
copy(TrueExe.path(), "dir1/prog");
copy(FalseExe.path(), "dir3/prog");
pb.environment().put("PATH","dir3");
equal(run(pb).exitValue(), True.exitValue());
copy(TrueExe.path(), "dir3/prog");
copy(FalseExe.path(), "dir1/prog");
equal(run(pb).exitValue(), False.exitValue());
} finally {
// cleanup
new File("dir1/prog").delete();
new File("dir2/prog").delete();
new File("dir3/prog").delete();
new File("dir1").delete();
new File("dir2").delete();
new File("dir3").delete();
new File("prog").delete();
}
}
if (failed != 0) throw new Error("PATH search algorithm");
}
else throw new Error("JavaChild invocation error");
}
}
private static void copy(String src, String dst) throws IOException {
Files.copy(Paths.get(src), Paths.get(dst),
StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
}
private static String javaChildOutput(ProcessBuilder pb, String...args) {
List<String> list = new ArrayList<String>(javaChildArgs);
for (String arg : args)
list.add(arg);
pb.command(list);
return commandOutput(pb);
}
private static String getenvInChild(ProcessBuilder pb) {
return javaChildOutput(pb, "System.getenv()");
}
private static String getenvInChild1234(ProcessBuilder pb) {
return javaChildOutput(pb, "System.getenv(\\u1234)");
}
private static String getenvInChild(ProcessBuilder pb, String name) {
return javaChildOutput(pb, "System.getenv(String)", name);
}
private static String pwdInChild(ProcessBuilder pb) {
return javaChildOutput(pb, "pwd");
}
private static final String javaExe =
System.getProperty("java.home") +
File.separator + "bin" + File.separator + "java";
private static final String classpath =
System.getProperty("java.class.path");
private static final List<String> javaChildArgs =
Arrays.asList(javaExe,
"-XX:+DisplayVMOutputToStderr",
"-classpath", absolutifyPath(classpath),
"Basic$JavaChild");
private static void testEncoding(String encoding, String tested) {
try {
// If round trip conversion works, should be able to set env vars
// correctly in child.
if (new String(tested.getBytes()).equals(tested)) {
out.println("Testing " + encoding + " environment values");
ProcessBuilder pb = new ProcessBuilder();
pb.environment().put("ASCIINAME",tested);
equal(getenvInChild(pb,"ASCIINAME"), tested);
}
} catch (Throwable t) { unexpected(t); }
}
static class Windows {
public static boolean is() { return is; }
private static final boolean is =
System.getProperty("os.name").startsWith("Windows");
}
static class AIX {
public static boolean is() { return is; }
private static final boolean is =
System.getProperty("os.name").equals("AIX");
}
static class Unix {
public static boolean is() { return is; }
private static final boolean is =
(! Windows.is() &&
new File("/bin/sh").exists() &&
new File("/bin/true").exists() &&
new File("/bin/false").exists());
}
static class UnicodeOS {
public static boolean is() { return is; }
private static final String osName = System.getProperty("os.name");
private static final boolean is =
// MacOS X would probably also qualify
osName.startsWith("Windows") &&
! osName.startsWith("Windows 9") &&
! osName.equals("Windows Me");
}
static class MacOSX {
public static boolean is() { return is; }
private static final String osName = System.getProperty("os.name");
private static final boolean is = osName.contains("OS X");
}
static class True {
public static int exitValue() { return 0; }
}
private static class False {
public static int exitValue() { return exitValue; }
private static final int exitValue = exitValue0();
private static int exitValue0() {
// /bin/false returns an *unspecified* non-zero number.
try {
if (! Unix.is())
return -1;
else {
int rc = new ProcessBuilder("/bin/false")
.start().waitFor();
check(rc != 0);
return rc;
}
} catch (Throwable t) { unexpected(t); return -1; }
}
}
// On Alpine Linux, /bin/true and /bin/false are just links to /bin/busybox.
// Some tests copy /bin/true and /bin/false to files with a different filename.
// However, copying the busbox executable into a file with a different name
// won't result in the expected return codes. As workaround, we create
// executable files that can be copied and produce the expected return
// values.
private static class TrueExe {
public static String path() { return path; }
private static final String path = path0();
private static String path0(){
if (!Platform.isBusybox("/bin/true")) {
return "/bin/true";
} else {
File trueExe = new File("true");
setFileContents(trueExe, "#!/bin/true\n");
trueExe.setExecutable(true);
return trueExe.getAbsolutePath();
}
}
}
private static class FalseExe {
public static String path() { return path; }
private static final String path = path0();
private static String path0(){
if (!Platform.isBusybox("/bin/false")) {
return "/bin/false";
} else {
File falseExe = new File("false");
setFileContents(falseExe, "#!/bin/false\n");
falseExe.setExecutable(true);
return falseExe.getAbsolutePath();
}
}
}
static class EnglishUnix {
private static final Boolean is =
(! Windows.is() && isEnglish("LANG") && isEnglish("LC_ALL"));
private static boolean isEnglish(String envvar) {
String val = getenv(envvar);
return (val == null) || val.matches("en.*") || val.matches("C");
}
/** Returns true if we can expect English OS error strings */
static boolean is() { return is; }
}
static class DelegatingProcess extends Process {
final Process p;
DelegatingProcess(Process p) {
this.p = p;
}
@Override
public void destroy() {
p.destroy();
}
@Override
public int exitValue() {
return p.exitValue();
}
@Override
public int waitFor() throws InterruptedException {
return p.waitFor();
}
@Override
public OutputStream getOutputStream() {
return p.getOutputStream();
}
@Override
public InputStream getInputStream() {
return p.getInputStream();
}
@Override
public InputStream getErrorStream() {
return p.getErrorStream();
}
}
private static boolean matches(String str, String regex) {
return Pattern.compile(regex).matcher(str).find();
}
private static String matchAndExtract(String str, String regex) {
Matcher matcher = Pattern.compile(regex).matcher(str);
if (matcher.find()) {
return matcher.group();
} else {
return "";
}
}
/* Only used for Mac OS X --
* Mac OS X (may) add the variable __CF_USER_TEXT_ENCODING to an empty
* environment. The environment variable JAVA_MAIN_CLASS_<pid> may also
* be set in Mac OS X.
* Remove them both from the list of env variables
*/
private static String removeMacExpectedVars(String vars) {
// Check for __CF_USER_TEXT_ENCODING
String cleanedVars = vars.replace("__CF_USER_TEXT_ENCODING="
+cfUserTextEncoding+",","");
// Check for JAVA_MAIN_CLASS_<pid>
String javaMainClassStr
= matchAndExtract(cleanedVars,
"JAVA_MAIN_CLASS_\\d+=Basic.JavaChild,");
return cleanedVars.replace(javaMainClassStr,"");
}
/* Only used for AIX --
* AIX adds the variable AIXTHREAD_GUARDPAGES=0 to the environment.
* Remove it from the list of env variables
*/
private static String removeAixExpectedVars(String vars) {
return vars.replace("AIXTHREAD_GUARDPAGES=0,", "");
}
private static String sortByLinesWindowsly(String text) {
String[] lines = text.split("\n");
Arrays.sort(lines, new WindowsComparator());
StringBuilder sb = new StringBuilder();
for (String line : lines)
sb.append(line).append("\n");
return sb.toString();
}
private static void checkMapSanity(Map<String,String> map) {
try {
Set<String> keySet = map.keySet();
Collection<String> values = map.values();
Set<Map.Entry<String,String>> entrySet = map.entrySet();
equal(entrySet.size(), keySet.size());
equal(entrySet.size(), values.size());
StringBuilder s1 = new StringBuilder();
for (Map.Entry<String,String> e : entrySet)
s1.append(e.getKey() + "=" + e.getValue() + "\n");
StringBuilder s2 = new StringBuilder();
for (String var : keySet)
s2.append(var + "=" + map.get(var) + "\n");
equal(s1.toString(), s2.toString());
Iterator<String> kIter = keySet.iterator();
Iterator<String> vIter = values.iterator();
Iterator<Map.Entry<String,String>> eIter = entrySet.iterator();
while (eIter.hasNext()) {
Map.Entry<String,String> entry = eIter.next();
String key = kIter.next();
String value = vIter.next();
check(entrySet.contains(entry));
check(keySet.contains(key));
check(values.contains(value));
check(map.containsKey(key));
check(map.containsValue(value));
equal(entry.getKey(), key);
equal(entry.getValue(), value);
}
check(!kIter.hasNext() &&
!vIter.hasNext());
} catch (Throwable t) { unexpected(t); }
}
private static void checkMapEquality(Map<String,String> map1,
Map<String,String> map2) {
try {
equal(map1.size(), map2.size());
equal(map1.isEmpty(), map2.isEmpty());
for (String key : map1.keySet()) {
equal(map1.get(key), map2.get(key));
check(map2.keySet().contains(key));
}
equal(map1, map2);
equal(map2, map1);
equal(map1.entrySet(), map2.entrySet());
equal(map2.entrySet(), map1.entrySet());
equal(map1.keySet(), map2.keySet());
equal(map2.keySet(), map1.keySet());
equal(map1.hashCode(), map2.hashCode());
equal(map1.entrySet().hashCode(), map2.entrySet().hashCode());
equal(map1.keySet().hashCode(), map2.keySet().hashCode());
} catch (Throwable t) { unexpected(t); }
}
static void checkRedirects(ProcessBuilder pb,
Redirect in, Redirect out, Redirect err) {
equal(pb.redirectInput(), in);
equal(pb.redirectOutput(), out);
equal(pb.redirectError(), err);
}
static void redirectIO(ProcessBuilder pb,
Redirect in, Redirect out, Redirect err) {
pb.redirectInput(in);
pb.redirectOutput(out);
pb.redirectError(err);
}
static void setFileContents(File file, String contents) {
try {
Writer w = new FileWriter(file);
w.write(contents);
w.close();
} catch (Throwable t) { unexpected(t); }
}
static String fileContents(File file) {
try {
Reader r = new FileReader(file);
StringBuilder sb = new StringBuilder();
char[] buffer = new char[1024];
int n;
while ((n = r.read(buffer)) != -1)
sb.append(buffer,0,n);
r.close();
return new String(sb);
} catch (Throwable t) { unexpected(t); return ""; }
}
static void testIORedirection() throws Throwable {
final File ifile = new File("ifile");
final File ofile = new File("ofile");
final File efile = new File("efile");
ifile.delete();
ofile.delete();
efile.delete();
//----------------------------------------------------------------
// Check mutual inequality of different types of Redirect
//----------------------------------------------------------------
Redirect[] redirects =
{ PIPE,
INHERIT,
DISCARD,
Redirect.from(ifile),
Redirect.to(ifile),
Redirect.appendTo(ifile),
Redirect.from(ofile),
Redirect.to(ofile),
Redirect.appendTo(ofile),
};
for (int i = 0; i < redirects.length; i++)
for (int j = 0; j < redirects.length; j++)
equal(redirects[i].equals(redirects[j]), (i == j));
//----------------------------------------------------------------
// Check basic properties of different types of Redirect
//----------------------------------------------------------------
equal(PIPE.type(), Redirect.Type.PIPE);
equal(PIPE.toString(), "PIPE");
equal(PIPE.file(), null);
equal(INHERIT.type(), Redirect.Type.INHERIT);
equal(INHERIT.toString(), "INHERIT");
equal(INHERIT.file(), null);
equal(DISCARD.type(), Redirect.Type.WRITE);
equal(DISCARD.toString(), "WRITE");
equal(DISCARD.file(), new File((Windows.is() ? "NUL" : "/dev/null")));
equal(Redirect.from(ifile).type(), Redirect.Type.READ);
equal(Redirect.from(ifile).toString(),
"redirect to read from file \"ifile\"");
equal(Redirect.from(ifile).file(), ifile);
equal(Redirect.from(ifile),
Redirect.from(ifile));
equal(Redirect.from(ifile).hashCode(),
Redirect.from(ifile).hashCode());
equal(Redirect.to(ofile).type(), Redirect.Type.WRITE);
equal(Redirect.to(ofile).toString(),
"redirect to write to file \"ofile\"");
equal(Redirect.to(ofile).file(), ofile);
equal(Redirect.to(ofile),
Redirect.to(ofile));
equal(Redirect.to(ofile).hashCode(),
Redirect.to(ofile).hashCode());
equal(Redirect.appendTo(ofile).type(), Redirect.Type.APPEND);
equal(Redirect.appendTo(efile).toString(),
"redirect to append to file \"efile\"");
equal(Redirect.appendTo(efile).file(), efile);
equal(Redirect.appendTo(efile),
Redirect.appendTo(efile));
equal(Redirect.appendTo(efile).hashCode(),
Redirect.appendTo(efile).hashCode());
//----------------------------------------------------------------
// Check initial values of redirects
//----------------------------------------------------------------
List<String> childArgs = new ArrayList<String>(javaChildArgs);
childArgs.add("testIO");
final ProcessBuilder pb = new ProcessBuilder(childArgs);
checkRedirects(pb, PIPE, PIPE, PIPE);
//----------------------------------------------------------------
// Check inheritIO
//----------------------------------------------------------------
pb.inheritIO();
checkRedirects(pb, INHERIT, INHERIT, INHERIT);
//----------------------------------------------------------------
// Check DISCARD for stdout,stderr
//----------------------------------------------------------------
redirectIO(pb, INHERIT, DISCARD, DISCARD);
checkRedirects(pb, INHERIT, DISCARD, DISCARD);
//----------------------------------------------------------------
// Check setters and getters agree
//----------------------------------------------------------------
pb.redirectInput(ifile);
equal(pb.redirectInput().file(), ifile);
equal(pb.redirectInput(), Redirect.from(ifile));
pb.redirectOutput(ofile);
equal(pb.redirectOutput().file(), ofile);
equal(pb.redirectOutput(), Redirect.to(ofile));