--- old/test/jdk/java/io/File/GetXSpace.java 2020-08-11 11:31:36.000000000 -0700 +++ new/test/jdk/java/io/File/GetXSpace.java 2020-08-11 11:31:36.000000000 -0700 @@ -24,11 +24,9 @@ /** * @test * @bug 4057701 6286712 6364377 - * @requires (os.family == "linux" | os.family == "mac" | os.family == "windows") - * @run build GetXSpace - * @run shell GetXSpace.sh + * @requires (os.family == "linux" | os.family == "mac" | + * os.family == "windows") * @summary Basic functionality of File.get-X-Space methods. - * @key randomness */ import java.io.BufferedReader; @@ -38,11 +36,13 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.FileStore; +import java.nio.file.Path; import java.security.Permission; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static java.lang.System.err; import static java.lang.System.out; public class GetXSpace { @@ -61,6 +61,12 @@ private static int pass = 0; private static Throwable first; + static void reset() { + fail = 0; + pass = 0; + first = null; + } + static void pass() { pass++; } @@ -338,7 +344,8 @@ } } - private static void testFile(String dirName) { + private static int testFile(Path dir) { + String dirName = dir.toString(); out.format("--- Testing %s%n", dirName); ArrayList l; try { @@ -347,9 +354,18 @@ throw new RuntimeException(dirName + " can't get file system information", x); } compare(l.get(0)); + + if (fail != 0) { + err.format("%d tests: %d failure(s); first: %s%n", + fail + pass, fail, first); + } else { + out.format("all %d tests passed%n", fail + pass); + } + + return fail != 0 ? 1 : 0; } - private static void testDF() { + private static int testDF() { out.println("--- Testing df"); // Find all of the partitions on the machine and verify that the size // returned by "df" is equivalent to File.getXSpace() values. @@ -381,20 +397,51 @@ } } } - } - public static void main(String [] args) { - if (args.length > 0) { - testFile(args[0]); - } else { - testDF(); - } + System.setSecurityManager(null); if (fail != 0) { - throw new RuntimeException((fail + pass) + " tests: " - + fail + " failure(s), first", first); + err.format("%d tests: %d failure(s); first: %s%n", + fail + pass, fail, first); } else { out.format("all %d tests passed%n", fail + pass); } + + return fail != 0 ? 1 : 0; + } + + private static void perms(File file, boolean allow) throws IOException { + file.setExecutable(allow, false); + file.setReadable(allow, false); + file.setWritable(allow, false); + } + + private static void deny(Path path) throws IOException { + perms(path.toFile(), false); + } + + private static void allow(Path path) throws IOException { + perms(path.toFile(), true); + } + + public static void main(String[] args) throws Exception { + int failedTests = testDF(); + reset(); + + Path tmpDir = Files.createTempDirectory(null); + Path tmpSubdir = Files.createTempDirectory(tmpDir, null); + Path tmpFile = Files.createTempFile(tmpSubdir, "foo", null); + + deny(tmpSubdir); + failedTests += testFile(tmpFile); + + allow(tmpSubdir); + Files.delete(tmpFile); + Files.delete(tmpSubdir); + Files.delete(tmpDir); + + if (failedTests > 0) { + throw new RuntimeException(failedTests + " test(s) failed"); + } } }