harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r377506 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/FilePermission.java test/java/org/apache/harmony/tests/java/io/AllTests.java test/java/org/apache/harmony/tests/java/io/FilePermissionTest.java
Date Mon, 13 Feb 2006 21:55:10 GMT
Author: tellison
Date: Mon Feb 13 13:55:09 2006
New Revision: 377506

URL: http://svn.apache.org/viewcvs?rev=377506&view=rev
Log:
Fix for HARMONY-47 (java.io.FilePermissions should pay attention to system file separator)

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/FilePermissionTest.java
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FilePermission.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FilePermission.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FilePermission.java?rev=377506&r1=377505&r2=377506&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FilePermission.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FilePermission.java
Mon Feb 13 13:55:09 2006
@@ -89,18 +89,10 @@
 									}
 								}
 							});
-					int plength = canonPath.length();
-					if (plength >= 1) {
-						if (canonPath.endsWith("*")) { //$NON-NLS-1$
-							if (plength == 1
-									|| (canonPath.charAt(plength - 2)) == File.separatorChar)
-								allDir = true;
-						} else if (canonPath.endsWith("-")) { //$NON-NLS-1$
-							if (plength == 1
-									|| (canonPath.charAt(plength - 2)) == File.separatorChar)
-								allSubdir = true;
-						}
-					}
+					if (path.equals("*") || path.endsWith(File.separator + "*"))
+						allDir = true;
+					if (path.equals("-") || path.endsWith(File.separator + "-"))
+						allSubdir = true;
 				}
 				this.actions = toCanonicalActionString(pathActions);
 			} else
@@ -254,65 +246,63 @@
 		if (allSubdir && thisLength == 2
 				&& !fp.canonPath.equals(File.separator))
 			return matchedMask;
+		// need /- to imply /-
+		if (fp.allSubdir && !allSubdir)
+			return 0;
+		// need /- or /* to imply /*
+		if (fp.allDir && !allSubdir && !allDir)
+			return 0;
 
 		boolean includeDir = false;
-		boolean lastIsSlash = false;
 		int pLength = fp.canonPath.length();
+		// do not compare the * or -
+		if (allDir || allSubdir)
+			thisLength--;
+		if (fp.allDir || fp.allSubdir)
+			pLength--;
 		for (int i = 0; i < pLength; i++) {
 			char pChar = fp.canonPath.charAt(i);
 			// Is p longer than this permissions canonLength?
 			if (i >= thisLength) {
+				if (i == thisLength) {
+					// Is this permission include all? (must have matched up
+					// until this point).
+					if (allSubdir)
+						return matchedMask;
+					// Is this permission include a dir? Continue the check
+					// afterwards.
+					if (allDir)
+						includeDir = true;
+				}
 				// If not includeDir then is has to be a mismatch.
 				if (!includeDir)
 					return 0;
 				/**
-				 * If we have * for this and the separator is not the last char
-				 * it is invalid. IE: this is '/a/*' and p is '/a/b/c' we should
-				 * fail on the separator after the b.
+				 * If we have * for this and find a separator it is invalid. IE:
+				 * this is '/a/*' and p is '/a/b/c' we should fail on the
+				 * separator after the b. Except for root, canonical paths do
+				 * not end in a separator.
 				 */
-				if (pChar == File.separatorChar && (i != pLength - 1))
+				if (pChar == File.separatorChar)
 					return 0;
 			} else {
-				// Can safely get cChar since it's in range.
-				char cChar = canonPath.charAt(i);
-				// Is this permission include all? (must have matched up until
-				// this point).
-				if (lastIsSlash && cChar == '-') {
-					// Checked at constructor for separator/-
-					if (!allSubdir)
-						return 0;
-					// If we've already seen '*' return 0, can't group - and *.
-					if (includeDir)
-						return 0;
-					return matchedMask;
-				}
-				// Is this permission include a dir? Continue the check
-				// afterwards.
-				if (lastIsSlash && cChar == '*') {
-					/* Checked at constructor for File.separator/* */
-					if (!allDir)
-						return 0;
-					// Cannot have two *'s in a row.
-					if (includeDir)
-						return 0;
-					// * does not match -
-					if (fp.allSubdir)
-						return 0;
-					// Set the fact that we have seen a * in this permission.
-					includeDir = true;
-					continue;
-				}
 				// Are the characters matched?
-				if (cChar != pChar)
+				if (canonPath.charAt(i) != pChar)
 					return 0;
-				// Is is a separator char? Needed for /* and /-
-				lastIsSlash = cChar == File.separatorChar;
 			}
 		}
 		// Must have matched upto this point or it's a valid file in an include
 		// all directory
-		return pLength == thisLength || includeDir ? matchedMask : 0;
-	}
+		if (pLength == thisLength) {
+			if (allSubdir) {
+				// /- implies /- or /*
+				return fp.allSubdir || fp.allDir ? matchedMask : 0;
+			} else {
+				return allDir == fp.allDir ? matchedMask : 0;
+			}
+		}
+		return includeDir ? matchedMask : 0;
+}
 
 	/**
 	 * Answers a new PermissionCollection in which to place FilePermission

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java?rev=377506&r1=377505&r2=377506&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/AllTests.java
Mon Feb 13 13:55:09 2006
@@ -28,6 +28,7 @@
 		TestSuite suite = new TestSuite(
 				"Test for org.apache.harmony.tests.java.io");
 		//$JUnit-BEGIN$
+		suite.addTestSuite(FilePermissionTest.class);
 		suite.addTestSuite(FileTest.class);
 		suite.addTestSuite(BufferedReaderTest.class);
 		suite.addTestSuite(PushBackInputStreamTest.class);

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/FilePermissionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/FilePermissionTest.java?rev=377506&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/FilePermissionTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/io/FilePermissionTest.java
Mon Feb 13 13:55:09 2006
@@ -0,0 +1,39 @@
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.harmony.tests.java.io;
+
+import java.io.File;
+import java.io.FilePermission;
+
+import junit.framework.TestCase;
+
+public class FilePermissionTest extends TestCase {
+
+	/**
+	 * @tests java.io.FilePermission#implies(java.security.Permission)
+	 */
+	public void test_impliesLjava_io_FilePermission() {
+		// Regression for HARMONY-47
+		char separator = File.separatorChar;
+		char nonSeparator = (separator == '/') ? '\\' : '/';
+
+		FilePermission fp1 = new FilePermission(nonSeparator + "*", "read");
+		FilePermission fp2 = new FilePermission(separator + "a", "read");
+		assertFalse("Assert 0: non-separator worked", fp1.implies(fp2));
+		fp1 = new FilePermission(nonSeparator + "-", "read");
+		assertFalse("Assert 1: non-separator worked", fp1.implies(fp2));
+	}
+}



Mime
View raw message