harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From regi...@apache.org
Subject svn commit: r932003 - in /harmony/enhanced/java/branches/java6/classlib/modules/luni/src: main/java/java/io/File.java test/api/common/org/apache/harmony/luni/tests/java/io/FileTest.java
Date Thu, 08 Apr 2010 16:56:19 GMT
Author: regisxu
Date: Thu Apr  8 16:56:19 2010
New Revision: 932003

URL: http://svn.apache.org/viewvc?rev=932003&view=rev
Log:
Apply patch for HARMONY-6495: [java6][classlib][luni] File.canExecute throws SecurityException
when file doesn't have read permission

Modified:
    harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/io/File.java
    harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/FileTest.java

Modified: harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/io/File.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/io/File.java?rev=932003&r1=932002&r2=932003&view=diff
==============================================================================
--- harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/io/File.java
(original)
+++ harmony/enhanced/java/branches/java6/classlib/modules/luni/src/main/java/java/io/File.java
Thu Apr  8 16:56:19 2010
@@ -1703,7 +1703,10 @@ public class File implements Serializabl
      */
     public boolean canExecute() {
         checkExec();
-        return exists() && isExecutableImpl(properPath(true));
+        if (path.length() == 0) {
+            return false;
+        }
+        return existsImpl(properPath(true)) && isExecutableImpl(properPath(true));
     }
 
     private native boolean isExecutableImpl(byte[] filePath);

Modified: harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/FileTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/FileTest.java?rev=932003&r1=932002&r2=932003&view=diff
==============================================================================
--- harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/FileTest.java
(original)
+++ harmony/enhanced/java/branches/java6/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/FileTest.java
Thu Apr  8 16:56:19 2010
@@ -21,6 +21,7 @@ import java.io.File;
 import java.io.FileFilter;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.FilePermission;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.ObjectStreamClass;
@@ -30,6 +31,12 @@ import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.security.CodeSource;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.Permissions;
+import java.security.Policy;
+import java.security.ProtectionDomain;
 
 import junit.framework.TestCase;
 
@@ -2257,4 +2264,305 @@ public class FileTest extends TestCase {
         File file = new File("FileTest.golden.ser");
         SerializationTest.verifyGolden(this, file);
     }
+
+    /**
+     * @tests file methods with a security manager
+     * Regression for HARMONY-6495
+     */
+    public void test_with_security_manager() throws IOException {
+        // create a file
+        File file = new File("fileForSecurityTest");
+        
+        // save old security manager and policy to restore
+        SecurityManager oldManager = System.getSecurityManager();
+        Policy oldPolicy = Policy.getPolicy();
+        
+        // create a policy
+        MockPolicy customPolicy = new MockPolicy();
+        Policy.setPolicy(customPolicy);
+        
+        SecurityManager sm = new SecurityManager();
+        System.setSecurityManager(sm);
+
+        try {
+            
+            // try operations requiring execute permission
+            try {
+                file.canExecute();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            // grant all files execute permission
+            customPolicy.permissions.add(new FilePermission("<<ALL FILES>>",
"execute"));
+            file.canExecute();
+            
+            // try operations requiring write permission
+            try {
+                file.canWrite();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.createNewFile();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.mkdir();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.mkdirs();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.renameTo(new File("something"));
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.renameTo(new File("something"));
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.setLastModified(0);
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.setReadOnly();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.setWritable(true);
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.setReadable(true);
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.setExecutable(true);
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                File.createTempFile("abc", "d");
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            // grant all files write permission
+            customPolicy.permissions = new Permissions();
+            customPolicy.permissions.add(new FilePermission("<<ALL FILES>>",
"write"));
+            // check canWrite()
+            file.canWrite();
+
+            // try operations requiring read permission
+            try {
+                file.canRead();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.exists();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.isFile();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.isDirectory();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.isHidden();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.lastModified();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.length();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.list();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.listFiles();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.mkdirs();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.getTotalSpace();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.getFreeSpace();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.getUsableSpace();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            // try operations requiring delete permission
+            try {
+                file.delete();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.deleteOnExit();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            // grant all files delete permission
+            customPolicy.permissions = new Permissions();
+            customPolicy.permissions.add(new FilePermission("<<ALL FILES>>",
"delete"));
+            // try delete()
+            file.delete();
+            
+            // grant all files read permission
+            customPolicy.permissions = new Permissions();
+            customPolicy.permissions.add(new FilePermission("<<ALL FILES>>",
"read"));
+            // check canRead()
+            file.canRead();
+
+            // try operations requiring read and getFileSystemAttributes runtime permission
+            try {
+                file.getTotalSpace();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.getFreeSpace();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            try {
+                file.getUsableSpace();
+                fail("Expected security exception");
+            } catch (SecurityException e) {
+                // expected
+            }
+            
+            // grant getFileSystemAttributes
+            customPolicy.permissions.add(new RuntimePermission("getFileSystemAttributes"));
+            file.getTotalSpace();
+            file.getFreeSpace();
+            file.getUsableSpace();
+            
+        } finally {
+            // restore security settings
+            System.setSecurityManager(oldManager);
+            Policy.setPolicy(oldPolicy);
+        }
+    }
+    
+    class MockPolicy extends Policy {
+        public Permissions permissions;
+        
+        public MockPolicy() {
+            permissions = new Permissions();
+        }
+        
+        public PermissionCollection getPermissions(CodeSource codesource) {
+            return permissions;
+        }
+        
+        public PermissionCollection getPermissions(ProtectionDomain domain) {
+            return permissions;
+        }
+        
+        public boolean implies(ProtectionDomain domain, Permission permission) {
+            if (permission.equals(new RuntimePermission("setSecurityManager"))) {
+                return true;
+            }
+            return permissions.implies(permission);
+        }
+    }
 }



Mime
View raw message