harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ton...@apache.org
Subject svn commit: r725920 - in /harmony/enhanced/classlib/trunk/modules/security/src: main/java/common/java/security/UnresolvedPermission.java test/impl/java.injected/java/security/UnresolvedPermission_ImplTest.java
Date Fri, 12 Dec 2008 06:38:12 GMT
Author: tonywu
Date: Thu Dec 11 22:38:12 2008
New Revision: 725920

URL: http://svn.apache.org/viewvc?rev=725920&view=rev
Log: (empty)

Modified:
    harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/UnresolvedPermission.java
    harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermission_ImplTest.java

Modified: harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/UnresolvedPermission.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/UnresolvedPermission.java?rev=725920&r1=725919&r2=725920&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/UnresolvedPermission.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/main/java/common/java/security/UnresolvedPermission.java
Thu Dec 11 22:38:12 2008
@@ -24,18 +24,14 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
-import java.io.InvalidObjectException;
 import java.io.NotSerializableException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-import java.io.ObjectStreamField;
 import java.io.Serializable;
 import java.security.cert.Certificate;
 import java.security.cert.CertificateEncodingException;
 import java.security.cert.CertificateException;
 import java.security.cert.CertificateFactory;
-import java.util.ArrayList;
-import java.util.List;
 
 import org.apache.harmony.security.fortress.PolicyUtils;
 import org.apache.harmony.security.internal.nls.Messages;
@@ -57,7 +53,6 @@
      */
     private static final long serialVersionUID = -4821973115467008846L;
 
-
     private String type;    
     
     private String name;
@@ -88,19 +83,7 @@
         this.type = type;
         this.name = name;
         this.actions = actions;
-        if (certs != null && certs.length != 0) {
-            //TODO filter non-signer certificates ???
-            List tmp = new ArrayList();
-            for (int i = 0; i < certs.length; i++) {
-                if (certs[i] != null) {
-                    tmp.add(certs[i]);
-                }
-            }
-            if (tmp.size() != 0) {
-                targetCerts = (Certificate[])tmp.toArray(
-                                new Certificate[tmp.size()]);
-            }
-        }
+        this.targetCerts = certs;
         hash = 0;
     }
 
@@ -137,21 +120,67 @@
             return true;
         }
         if (obj instanceof UnresolvedPermission) {
-            UnresolvedPermission that = (UnresolvedPermission)obj;
+            UnresolvedPermission that = (UnresolvedPermission) obj;
             if (getName().equals(that.getName())
-                && (name == null ? that.name == null 
-                    : name.equals(that.name))
-                && (actions == null ? that.actions == null
-                    : actions.equals(that.actions))
-                && (PolicyUtils.matchSubset(targetCerts, that.targetCerts) 
-                    && PolicyUtils.matchSubset(that.targetCerts, targetCerts))) {
+                    && (name == null ? that.name == null : name
+                            .equals(that.name))
+                    && (actions == null ? that.actions == null : actions
+                            .equals(that.actions))
+                    && equalsCertificates(this.targetCerts, that.targetCerts)) {
                 return true;
             }
         }
         return false;
     }
 
-	/**
+    /*
+     * check whether given array of certificates are equivalent
+     */
+    private boolean equalsCertificates(Certificate[] certs1,
+            Certificate[] certs2) {
+        if (certs1 == null || certs2 == null) {
+            return certs1 == certs2;
+        }
+
+        int length = certs1.length;
+        if (length != certs2.length) {
+            return false;
+        }
+
+        if (length > 0) {
+            boolean found;
+            for (int i = 0; i < length; i++) {
+                found = false;
+                for (int j = 0; j < length; j++) {
+                    if (certs1[i].equals(certs2[j])) {
+                        found = true;
+                        break;
+                    }
+                }
+
+                if (!found) {
+                    return false;
+                }
+            }
+
+            for (int i = 0; i < length; i++) {
+                found = false;
+                for (int j = 0; j < length; j++) {
+                    if (certs2[i].equals(certs1[j])) {
+                        found = true;
+                        break;
+                    }
+                }
+
+                if (!found) {
+                    return false;
+                }
+            }
+        }
+        return true;
+    }
+
+    /**
 	 * Answers an integer hash code for the receiver. Any two objects which
 	 * answer <code>true</code> when passed to <code>equals</code> must
 	 * answer the same value for this method.

Modified: harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermission_ImplTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermission_ImplTest.java?rev=725920&r1=725919&r2=725920&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermission_ImplTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermission_ImplTest.java
Thu Dec 11 22:38:12 2008
@@ -22,8 +22,12 @@
 
 package java.security;
 
+import java.security.cert.Certificate;
+
 import junit.framework.TestCase;
 
+import org.apache.harmony.security.tests.support.cert.MyCertificate;
+
 /**
  * Tests for <code>UnresolvedPermission</code> class fields and methods
  * 
@@ -46,14 +50,6 @@
         assertEquals(name, up.getUnresolvedName()); 
         assertEquals(action, up.getUnresolvedActions()); 
         assertNull(up.getUnresolvedCerts());
-        
-        up = new UnresolvedPermission(type, name, action, new java.security.cert.Certificate[0]);
-        assertNull("Empty array should be the same as null", up.getUnresolvedCerts());
-        // case of trivial collection: {null}
-        up = new UnresolvedPermission(type, name, action, new java.security.cert.Certificate[3]);
-        assertNull(up.getUnresolvedCerts());
-        //assertNotSame(up.getUnresolvedCerts(), up.getUnresolvedCerts());
-        //assertEquals(1, up.getUnresolvedCerts().length);
     }
     
     public void testEquals()
@@ -70,13 +66,10 @@
         assertTrue(up.equals(up2));
         assertTrue(up.hashCode() == up2.hashCode());
         up2 = new UnresolvedPermission(type, name, action, new java.security.cert.Certificate[0]);
-        assertTrue("null and empty certificates should be considered equal", up.equals(up2));
         assertTrue(up.hashCode() == up2.hashCode());
         up2 = new UnresolvedPermission(type, name, action, new java.security.cert.Certificate[2]);
-        assertTrue(up.equals(up2));
         //case of trivial collections {null} 
         up = new UnresolvedPermission(type, name, action, new java.security.cert.Certificate[10]);
-        assertTrue(up.equals(up2));
         assertTrue(up.hashCode() == up2.hashCode());
     }
 
@@ -102,4 +95,133 @@
         up = new UnresolvedPermission("java.security.AllPermission", null, null, new java.security.cert.Certificate[0]);
         assertEquals(new AllPermission(name, ""), up.resolve(AllPermission.class));
     }
+    
+    public static final String type = "java.util.PropertyPermission";
+
+    public static final String name = "os.name";
+
+    public static final String action = "write,read";
+
+    public static final byte[] testEncoding1 = new byte[] { (byte) 1 };
+
+    public static final byte[] testEncoding2 = new byte[] { (byte) 2 };
+
+    public static final byte[] testEncoding3 = new byte[] { (byte) 1, (byte) 2,
+            (byte) 3 };
+
+    public static final Certificate cert1 = new MyCertificate("TEST_TYPE1",
+            testEncoding1);
+
+    public static final Certificate cert2 = new MyCertificate("TEST_TYPE2",
+            testEncoding2);
+
+    public static final Certificate cert3 = new MyCertificate("TEST_TYPE3",
+            testEncoding3);
+
+    public void test_Constructor() {
+        UnresolvedPermission up = new UnresolvedPermission(type, name, action,
+                null);
+        assertNull(up.getUnresolvedCerts());
+
+        up = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[0]);
+        assertEquals(0, up.getUnresolvedCerts().length);
+
+        up = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[2]);
+        assertEquals(2, up.getUnresolvedCerts().length);
+    }
+
+    public void test_Equals_Scenario0() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                null);
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                null);
+        assertEquals(up1, up2);
+    }
+
+    public void test_Equals_Scenario1() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                null);
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[0]);
+        assertFalse(up1.equals(up2));
+    }
+
+    public void test_Equals_Scenario2() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                null);
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[2]);
+        assertFalse(up1.equals(up2));
+    }
+
+    public void test_Equals_Scenario3() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[0]);
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[0]);
+        assertEquals(up1, up2);
+    }
+
+    public void test_Equals_Scenario4() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[0]);
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[2]);
+        assertFalse(up1.equals(up2));
+    }
+
+    public void test_Equals_Scenario5() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[2]);
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[5]);
+        assertFalse(up1.equals(up2));
+    }
+
+    public void test_Equals_Scenario6() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[] { cert1 });
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[] { cert1 });
+        assertEquals(up1, up2);
+    }
+
+    public void test_Equals_Scenario7() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[] { cert1, cert2 });
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[] { cert1, cert2 });
+        assertEquals(up1, up2);
+    }
+
+    public void test_Equals_Scenario8() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[] { cert1, cert2 });
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[] { cert2, cert1 });
+        assertEquals(up1, up2);
+    }
+
+    public void test_Equals_Scenario9() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[] { cert1, cert2, cert3 });
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[] { cert2, cert1, cert3 });
+        assertEquals(up1, up2);
+    }
+    
+    public void test_Equals_Scenario10() {
+        UnresolvedPermission up1 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[2]);
+        UnresolvedPermission up2 = new UnresolvedPermission(type, name, action,
+                new java.security.cert.Certificate[2]);
+        try {
+            up1.equals(up2);
+            fail("should throw NullPointerException");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
 }



Mime
View raw message