8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option

Add to specification of newBufferedWriter() and write() methods that the DELETE_ON_CLOSE option triggers invoking checkDelete().

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2015-05-07 16:12:10 -07:00
parent 9a104f4c57
commit c0d7208b4b
2 changed files with 92 additions and 8 deletions

View File

@ -2851,7 +2851,10 @@ public final class Files {
* @throws SecurityException * @throws SecurityException
* In the case of the default provider, and a security manager is * In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkWrite(String) checkWrite} * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
* method is invoked to check write access to the file. * method is invoked to check write access to the file. The {@link
* SecurityManager#checkDelete(String) checkDelete} method is
* invoked to check delete access if the file is opened with the
* {@code DELETE_ON_CLOSE} option.
* *
* @see #write(Path,Iterable,Charset,OpenOption[]) * @see #write(Path,Iterable,Charset,OpenOption[])
*/ */
@ -2893,7 +2896,10 @@ public final class Files {
* @throws SecurityException * @throws SecurityException
* In the case of the default provider, and a security manager is * In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkWrite(String) checkWrite} * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
* method is invoked to check write access to the file. * method is invoked to check write access to the file. The {@link
* SecurityManager#checkDelete(String) checkDelete} method is
* invoked to check delete access if the file is opened with the
* {@code DELETE_ON_CLOSE} option.
* *
* @since 1.8 * @since 1.8
*/ */
@ -3290,7 +3296,10 @@ public final class Files {
* @throws SecurityException * @throws SecurityException
* In the case of the default provider, and a security manager is * In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkWrite(String) checkWrite} * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
* method is invoked to check write access to the file. * method is invoked to check write access to the file. The {@link
* SecurityManager#checkDelete(String) checkDelete} method is
* invoked to check delete access if the file is opened with the
* {@code DELETE_ON_CLOSE} option.
*/ */
public static Path write(Path path, byte[] bytes, OpenOption... options) public static Path write(Path path, byte[] bytes, OpenOption... options)
throws IOException throws IOException
@ -3350,7 +3359,10 @@ public final class Files {
* @throws SecurityException * @throws SecurityException
* In the case of the default provider, and a security manager is * In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkWrite(String) checkWrite} * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
* method is invoked to check write access to the file. * method is invoked to check write access to the file. The {@link
* SecurityManager#checkDelete(String) checkDelete} method is
* invoked to check delete access if the file is opened with the
* {@code DELETE_ON_CLOSE} option.
*/ */
public static Path write(Path path, Iterable<? extends CharSequence> lines, public static Path write(Path path, Iterable<? extends CharSequence> lines,
Charset cs, OpenOption... options) Charset cs, OpenOption... options)
@ -3398,7 +3410,10 @@ public final class Files {
* @throws SecurityException * @throws SecurityException
* In the case of the default provider, and a security manager is * In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkWrite(String) checkWrite} * installed, the {@link SecurityManager#checkWrite(String) checkWrite}
* method is invoked to check write access to the file. * method is invoked to check write access to the file. The {@link
* SecurityManager#checkDelete(String) checkDelete} method is
* invoked to check delete access if the file is opened with the
* {@code DELETE_ON_CLOSE} option.
* *
* @since 1.8 * @since 1.8
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,7 +22,7 @@
*/ */
/* @test /* @test
* @bug 6866804 7006126 8028270 * @bug 6866804 7006126 8028270 8065109
* @summary Unit test for java.nio.file.Files * @summary Unit test for java.nio.file.Files
* @library .. * @library ..
* @build CheckPermissions * @build CheckPermissions
@ -37,6 +37,7 @@ import java.nio.file.attribute.*;
import java.nio.channels.SeekableByteChannel; import java.nio.channels.SeekableByteChannel;
import java.security.Permission; import java.security.Permission;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
/** /**
@ -426,8 +427,40 @@ public class CheckPermissions {
} }
createFile(file); // restore file createFile(file); // restore file
// -- newBufferedReader/newBufferedWriter --
// -- newInputStream/newOutptuStream -- prepare();
try (BufferedReader br = newBufferedReader(file)) {
assertCheckRead(file);
}
prepare();
try (BufferedWriter bw = newBufferedWriter(file, WRITE)) {
assertCheckWrite(file);
}
prepare();
try (BufferedWriter bw = newBufferedWriter(file, DELETE_ON_CLOSE)) {
assertCheckWrite(file);
assertCheckDelete(file);
}
createFile(file); // restore file
prepare();
try (BufferedWriter bw = newBufferedWriter(file,
StandardCharsets.UTF_16, WRITE)) {
assertCheckWrite(file);
}
prepare();
try (BufferedWriter bw = newBufferedWriter(file,
StandardCharsets.UTF_16, DELETE_ON_CLOSE)) {
assertCheckWrite(file);
assertCheckDelete(file);
}
createFile(file); // restore file
// -- newInputStream/newOutputStream --
prepare(); prepare();
try (InputStream in = newInputStream(file)) { try (InputStream in = newInputStream(file)) {
@ -438,6 +471,42 @@ public class CheckPermissions {
assertCheckWrite(file); assertCheckWrite(file);
} }
// -- write --
prepare();
Files.write(file, new byte[]{(byte) 42, (byte) 666}, WRITE);
assertCheckWrite(file);
prepare();
Files.write(file, new byte[]{(byte) 42, (byte) 666}, WRITE,
DELETE_ON_CLOSE);
assertCheckWrite(file);
assertCheckDelete(file);
createFile(file); // restore file
List<String> lines = Arrays.asList("42", "666");
prepare();
Files.write(file, lines, StandardCharsets.UTF_16, WRITE);
assertCheckWrite(file);
prepare();
Files.write(file, lines, StandardCharsets.UTF_16, WRITE,
DELETE_ON_CLOSE);
assertCheckWrite(file);
assertCheckDelete(file);
createFile(file); // restore file
prepare();
Files.write(file, lines, WRITE);
assertCheckWrite(file);
prepare();
Files.write(file, lines, WRITE, DELETE_ON_CLOSE);
assertCheckWrite(file);
assertCheckDelete(file);
createFile(file); // restore file
// -- newDirectoryStream -- // -- newDirectoryStream --
prepare(); prepare();