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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4057701 6286712 6364377 27 * @requires (os.family == "linux" | os.family == "mac" | os.family == "windows") 28 * @run build GetXSpace 29 * @run shell GetXSpace.sh 30 * @summary Basic functionality of File.get-X-Space methods. 31 * @key randomness 32 */ 33 34 import java.io.BufferedReader; 35 import java.io.File; 36 import java.io.FilePermission; 37 import java.io.InputStreamReader; 38 import java.io.IOException; 39 import java.nio.file.Files; 40 import java.security.Permission; 41 import java.util.ArrayList; 42 import java.util.regex.Matcher; 43 import java.util.regex.Pattern; 44 45 import static java.lang.System.out; 46 47 public class GetXSpace { 48 49 private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(), 50 new DenyRead() }; 51 52 private static final String OS_NAME = System.getProperty("os.name"); 53 private static final boolean IS_MAC = OS_NAME.startsWith("Mac"); 54 private static final boolean IS_WIN = OS_NAME.startsWith("Windows"); 55 56 // FileSystem Total Used Available Use% MountedOn 57 private static final Pattern DF_PATTERN = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n"); 58 59 private static int fail = 0; 60 private static int pass = 0; 61 private static Throwable first; 62 63 static void pass() { 64 pass++; 65 } 66 67 static void fail(String p) { 68 setFirst(p); 69 System.err.format("FAILED: %s%n", p); 70 fail++; 71 } 72 73 static void fail(String p, long exp, String cmp, long got) { 74 String s = String.format("'%s': %d %s %d", p, exp, cmp, got); 75 setFirst(s); 76 System.err.format("FAILED: %s%n", s); 77 fail++; 78 } 79 80 private static void fail(String p, Class ex) { 81 String s = String.format("'%s': expected %s - FAILED%n", p, ex.getName()); 82 setFirst(s); 314 if (p.implies(new RuntimePermission("getFileSystemAttributes"))) 315 throw new SecurityException(err); 316 super.checkPermission(p); 317 } 318 319 public void checkPermission(Permission p, Object context) { 320 if (p.implies(new RuntimePermission("getFileSystemAttributes"))) 321 throw new SecurityException(err); 322 super.checkPermission(p, context); 323 } 324 } 325 326 private static class DenyRead extends Deny { 327 private String err = "sorry - checkRead()"; 328 329 public void checkRead(String file) { 330 throw new SecurityException(err); 331 } 332 } 333 334 private static void testFile(String dirName) { 335 out.format("--- Testing %s%n", dirName); 336 ArrayList<Space> l; 337 try { 338 l = space(dirName); 339 } catch (IOException x) { 340 throw new RuntimeException(dirName + " can't get file system information", x); 341 } 342 compare(l.get(0)); 343 } 344 345 private static void testDF() { 346 out.println("--- Testing df"); 347 // Find all of the partitions on the machine and verify that the size 348 // returned by "df" is equivalent to File.getXSpace() values. 349 ArrayList<Space> l; 350 try { 351 l = space(null); 352 } catch (IOException x) { 353 throw new RuntimeException("can't get file system information", x); 354 } 355 if (l.size() == 0) 356 throw new RuntimeException("no partitions?"); 357 358 for (int i = 0; i < sma.length; i++) { 359 System.setSecurityManager(sma[i]); 360 SecurityManager sm = System.getSecurityManager(); 361 if (sma[i] != null && sm == null) 362 throw new RuntimeException("Test configuration error " 363 + " - can't set security manager"); 364 365 out.format("%nSecurityManager = %s%n" , 366 (sm == null ? "null" : sm.getClass().getName())); 367 for (var s : l) { 368 if (sm instanceof Deny) { 369 tryCatch(s); 370 } else { 371 compare(s); 372 compareZeroNonExist(); 373 compareZeroExist(); 374 } 375 } 376 } 377 } 378 379 public static void main(String [] args) { 380 if (args.length > 0) { 381 testFile(args[0]); 382 } else { 383 testDF(); 384 } 385 386 if (fail != 0) { 387 throw new RuntimeException((fail + pass) + " tests: " 388 + fail + " failure(s), first", first); 389 } else { 390 out.format("all %d tests passed%n", fail + pass); 391 } 392 } 393 } | 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * @test 26 * @bug 4057701 6286712 6364377 27 * @requires (os.family == "linux" | os.family == "mac" | 28 * os.family == "windows") 29 * @summary Basic functionality of File.get-X-Space methods. 30 */ 31 32 import java.io.BufferedReader; 33 import java.io.File; 34 import java.io.FilePermission; 35 import java.io.InputStreamReader; 36 import java.io.IOException; 37 import java.nio.file.Files; 38 import java.nio.file.Path; 39 import java.security.Permission; 40 import java.util.ArrayList; 41 import java.util.regex.Matcher; 42 import java.util.regex.Pattern; 43 44 import static java.lang.System.err; 45 import static java.lang.System.out; 46 47 public class GetXSpace { 48 49 private static SecurityManager [] sma = { null, new Allow(), new DenyFSA(), 50 new DenyRead() }; 51 52 private static final String OS_NAME = System.getProperty("os.name"); 53 private static final boolean IS_MAC = OS_NAME.startsWith("Mac"); 54 private static final boolean IS_WIN = OS_NAME.startsWith("Windows"); 55 56 // FileSystem Total Used Available Use% MountedOn 57 private static final Pattern DF_PATTERN = Pattern.compile("([^\\s]+)\\s+(\\d+)\\s+\\d+\\s+(\\d+)\\s+\\d+%\\s+([^\\s].*)\n"); 58 59 private static int fail = 0; 60 private static int pass = 0; 61 private static Throwable first; 62 63 static void reset() { 64 fail = 0; 65 pass = 0; 66 first = null; 67 } 68 69 static void pass() { 70 pass++; 71 } 72 73 static void fail(String p) { 74 setFirst(p); 75 System.err.format("FAILED: %s%n", p); 76 fail++; 77 } 78 79 static void fail(String p, long exp, String cmp, long got) { 80 String s = String.format("'%s': %d %s %d", p, exp, cmp, got); 81 setFirst(s); 82 System.err.format("FAILED: %s%n", s); 83 fail++; 84 } 85 86 private static void fail(String p, Class ex) { 87 String s = String.format("'%s': expected %s - FAILED%n", p, ex.getName()); 88 setFirst(s); 320 if (p.implies(new RuntimePermission("getFileSystemAttributes"))) 321 throw new SecurityException(err); 322 super.checkPermission(p); 323 } 324 325 public void checkPermission(Permission p, Object context) { 326 if (p.implies(new RuntimePermission("getFileSystemAttributes"))) 327 throw new SecurityException(err); 328 super.checkPermission(p, context); 329 } 330 } 331 332 private static class DenyRead extends Deny { 333 private String err = "sorry - checkRead()"; 334 335 public void checkRead(String file) { 336 throw new SecurityException(err); 337 } 338 } 339 340 private static int testFile(Path dir) { 341 String dirName = dir.toString(); 342 out.format("--- Testing %s%n", dirName); 343 ArrayList<Space> l; 344 try { 345 l = space(dirName); 346 } catch (IOException x) { 347 throw new RuntimeException(dirName + " can't get file system information", x); 348 } 349 compare(l.get(0)); 350 351 if (fail != 0) { 352 err.format("%d tests: %d failure(s); first: %s%n", 353 fail + pass, fail, first); 354 } else { 355 out.format("all %d tests passed%n", fail + pass); 356 } 357 358 return fail != 0 ? 1 : 0; 359 } 360 361 private static int testDF() { 362 out.println("--- Testing df"); 363 // Find all of the partitions on the machine and verify that the size 364 // returned by "df" is equivalent to File.getXSpace() values. 365 ArrayList<Space> l; 366 try { 367 l = space(null); 368 } catch (IOException x) { 369 throw new RuntimeException("can't get file system information", x); 370 } 371 if (l.size() == 0) 372 throw new RuntimeException("no partitions?"); 373 374 for (int i = 0; i < sma.length; i++) { 375 System.setSecurityManager(sma[i]); 376 SecurityManager sm = System.getSecurityManager(); 377 if (sma[i] != null && sm == null) 378 throw new RuntimeException("Test configuration error " 379 + " - can't set security manager"); 380 381 out.format("%nSecurityManager = %s%n" , 382 (sm == null ? "null" : sm.getClass().getName())); 383 for (var s : l) { 384 if (sm instanceof Deny) { 385 tryCatch(s); 386 } else { 387 compare(s); 388 compareZeroNonExist(); 389 compareZeroExist(); 390 } 391 } 392 } 393 394 System.setSecurityManager(null); 395 396 if (fail != 0) { 397 err.format("%d tests: %d failure(s); first: %s%n", 398 fail + pass, fail, first); 399 } else { 400 out.format("all %d tests passed%n", fail + pass); 401 } 402 403 return fail != 0 ? 1 : 0; 404 } 405 406 private static void perms(File file, boolean allow) throws IOException { 407 file.setExecutable(allow, false); 408 file.setReadable(allow, false); 409 file.setWritable(allow, false); 410 } 411 412 private static void deny(Path path) throws IOException { 413 perms(path.toFile(), false); 414 } 415 416 private static void allow(Path path) throws IOException { 417 perms(path.toFile(), true); 418 } 419 420 public static void main(String[] args) throws Exception { 421 int failedTests = testDF(); 422 reset(); 423 424 Path tmpDir = Files.createTempDirectory(null); 425 Path tmpSubdir = Files.createTempDirectory(tmpDir, null); 426 Path tmpFile = Files.createTempFile(tmpSubdir, "foo", null); 427 428 deny(tmpSubdir); 429 failedTests += testFile(tmpFile); 430 431 allow(tmpSubdir); 432 Files.delete(tmpFile); 433 Files.delete(tmpSubdir); 434 Files.delete(tmpDir); 435 436 if (failedTests > 0) { 437 throw new RuntimeException(failedTests + " test(s) failed"); 438 } 439 } 440 } |