8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
authorbpb
Thu, 07 May 2015 16:12:10 -0700
changeset 30379 42d3c3090d20
parent 30378 c631cf59315d
child 30380 aca452591e50
8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option Summary: Add to specification of newBufferedWriter() and write() methods that the DELETE_ON_CLOSE option triggers invoking checkDelete(). Reviewed-by: alanb
jdk/src/java.base/share/classes/java/nio/file/Files.java
jdk/test/java/nio/file/Files/CheckPermissions.java
--- a/jdk/src/java.base/share/classes/java/nio/file/Files.java	Thu May 07 08:55:17 2015 -0700
+++ b/jdk/src/java.base/share/classes/java/nio/file/Files.java	Thu May 07 16:12:10 2015 -0700
@@ -2851,7 +2851,10 @@
      * @throws  SecurityException
      *          In the case of the default provider, and a security manager is
      *          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[])
      */
@@ -2893,7 +2896,10 @@
      * @throws  SecurityException
      *          In the case of the default provider, and a security manager is
      *          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
      */
@@ -3290,7 +3296,10 @@
      * @throws  SecurityException
      *          In the case of the default provider, and a security manager is
      *          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)
         throws IOException
@@ -3350,7 +3359,10 @@
      * @throws  SecurityException
      *          In the case of the default provider, and a security manager is
      *          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,
                              Charset cs, OpenOption... options)
@@ -3398,7 +3410,10 @@
      * @throws  SecurityException
      *          In the case of the default provider, and a security manager is
      *          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
      */
--- a/jdk/test/java/nio/file/Files/CheckPermissions.java	Thu May 07 08:55:17 2015 -0700
+++ b/jdk/test/java/nio/file/Files/CheckPermissions.java	Thu May 07 16:12:10 2015 -0700
@@ -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.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 
 /* @test
- * @bug 6866804 7006126 8028270
+ * @bug 6866804 7006126 8028270 8065109
  * @summary Unit test for java.nio.file.Files
  * @library ..
  * @build CheckPermissions
@@ -37,6 +37,7 @@
 import java.nio.channels.SeekableByteChannel;
 import java.security.Permission;
 import java.io.*;
+import java.nio.charset.StandardCharsets;
 import java.util.*;
 
 /**
@@ -426,8 +427,40 @@
             }
             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();
             try (InputStream in = newInputStream(file)) {
@@ -438,6 +471,42 @@
                 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 --
 
             prepare();