You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ka...@apache.org on 2014/06/03 15:43:06 UTC

svn commit: r1599544 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests: tests/store/OSReadOnlyTest.java util/PrivilegedFileOpsForTests.java

Author: kahatlen
Date: Tue Jun  3 13:43:06 2014
New Revision: 1599544

URL: http://svn.apache.org/r1599544
Log:
DERBY-5824: Disable OSReadOnlyTest when run as privileged user

When running the test as root, it is able to modify the database even
though all database files have been made read-only. Disable the test
in such environments.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java?rev=1599544&r1=1599543&r2=1599544&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java Tue Jun  3 13:43:06 2014
@@ -22,6 +22,7 @@
 package org.apache.derbyTesting.functionTests.tests.store;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.security.AccessController;
@@ -106,6 +107,15 @@ public class OSReadOnlyTest extends Base
      * on OS level, the database reacts as if it's in 'ReadOnly' mode
      */
     public void testOSReadOnly() throws Exception {
+        if (!supportsSetReadOnly()) {
+            // If we can modify files after File.setReadOnly() has been
+            // called on them, the test database will not actually be
+            // read-only, and the test will fail. Skip the test in that
+            // case.
+            alarm("Read-only files can be modified. Skipping OSReadOnlyTest.");
+            return;
+        }
+
         // start with some simple checks
         setAutoCommit(false);
         Statement stmt = createStatement();
@@ -172,7 +182,39 @@ public class OSReadOnlyTest extends Base
         // testharness will try to remove the original db; put it back
         moveDatabaseOnOS("readOnly2", phDbName);
     }
-    
+
+    /**
+     * Check if {@code File.setReadOnly()} has any effect in this environment.
+     * For example, if the test runs as a privileged user, it may be able
+     * to modify a file even if it has been made read-only. If so, it doesn't
+     * make any sense to run this test.
+     *
+     * @return {@code true} if {@code File.setReadOnly()} prevents file
+     *   modifications; otherwise, {@code false}
+     * @throws IOException if an unexpected error happens
+     */
+    private boolean supportsSetReadOnly() throws IOException {
+        File tmp = PrivilegedFileOpsForTests.createTempFile(
+                "tmp", null, currentDirectory());
+        PrivilegedFileOpsForTests.setReadOnly(tmp);
+        FileOutputStream fs = null;
+        try {
+            fs = PrivilegedFileOpsForTests.getFileOutputStream(tmp);
+            // Was able to open the file in read-write mode, so it's not
+            // properly read-only.
+            return false;
+        } catch (FileNotFoundException fnf) {
+            // Failed to open the file in read-write mode, so it seems like
+            // it's read-only.
+            return true;
+        } finally {
+            if (fs != null) {
+                fs.close();
+            }
+            PrivilegedFileOpsForTests.delete(tmp);
+        }
+    }
+
     /*
      * figure out the physical database name, we want to manipulate
      * the actual files on the OS.

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java?rev=1599544&r1=1599543&r2=1599544&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java Tue Jun  3 13:43:06 2014
@@ -89,9 +89,35 @@ public class PrivilegedFileOpsForTests {
                         return file.getAbsolutePath();
                     }});
     }
-      
-    
-    
+
+    /**
+     * Create a temporary file.
+     *
+     * @param prefix file name prefix, at least three characters
+     * @param suffix file name suffix, defaults to ".tmp" if {@code null}
+     * @param directory where to create the file, or {@code null} for the
+     *   default temporary file directory
+     * @return the file that was created
+     * @throws IOException if the file cannot be created
+     * @see File#createTempFile(String, String, File)
+     */
+    public static File createTempFile(final String prefix,
+                                      final String suffix,
+                                      final File directory)
+            throws IOException
+    {
+        try {
+            return AccessController.doPrivileged(
+                new PrivilegedExceptionAction<File>() {
+                    @Override
+                    public File run() throws IOException {
+                        return File.createTempFile(prefix, suffix, directory);
+                    }
+                });
+        } catch (PrivilegedActionException pae) {
+            throw (IOException) pae.getCause();
+        }
+    }
 
     /**
      * Returns a input stream for the specified file.
@@ -193,6 +219,20 @@ public class PrivilegedFileOpsForTests {
     }
 
     /**
+     * Make a file or directory read-only.
+     * @param file the file to make read-only
+     * @return {@code true} if successful, {@code false} otherwise
+     */
+    public static boolean setReadOnly(final File file) {
+        return AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
+            @Override
+            public Boolean run() {
+                return file.setReadOnly();
+            }
+        });
+    }
+
+    /**
      * Obtains a reader for the specified file.
      *
      * @param file the file to obtain a reader for