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/12 10:55:42 UTC
svn commit: r1602096 - in /db/derby/code/branches/10.10: ./
java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java
java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
Author: kahatlen
Date: Thu Jun 12 08:55:42 2014
New Revision: 1602096
URL: http://svn.apache.org/r1602096
Log:
DERBY-5824: Disable OSReadOnlyTest when run as privileged user
Merged revision 1599544 from trunk.
Modified:
db/derby/code/branches/10.10/ (props changed)
db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java
db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
Propchange: db/derby/code/branches/10.10/
------------------------------------------------------------------------------
Merged /db/derby/code/trunk:r1599544
Modified: db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java?rev=1602096&r1=1602095&r2=1602096&view=diff
==============================================================================
--- db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java (original)
+++ db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/tests/store/OSReadOnlyTest.java Thu Jun 12 08:55:42 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/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java?rev=1602096&r1=1602095&r2=1602096&view=diff
==============================================================================
--- db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java (original)
+++ db/derby/code/branches/10.10/java/testing/org/apache/derbyTesting/functionTests/util/PrivilegedFileOpsForTests.java Thu Jun 12 08:55:42 2014
@@ -89,9 +89,34 @@ 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 (File) AccessController.doPrivileged(
+ new PrivilegedExceptionAction() {
+ public Object run() throws IOException {
+ return File.createTempFile(prefix, suffix, directory);
+ }
+ });
+ } catch (PrivilegedActionException pae) {
+ throw (IOException) pae.getCause();
+ }
+ }
/**
* Returns a input stream for the specified file.
@@ -162,6 +187,19 @@ 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 ((Boolean) AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ return Boolean.valueOf(file.setReadOnly());
+ }
+ })).booleanValue();
+ }
+
+ /**
* Obtains a reader for the specified file.
*
* @param file the file to obtain a reader for