You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ml...@apache.org on 2006/06/01 10:15:44 UTC
svn commit: r410788 [5/13] - in
/incubator/harmony/enhanced/classlib/trunk/modules: auth/make/common/
auth/src/test/java/common/javax/security/auth/
auth/src/test/java/common/javax/security/auth/callback/serialization/
auth/src/test/java/common/javax/s...
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/BasicPermissionCollectionTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/BasicPermissionCollectionTest.java?rev=410788&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/BasicPermissionCollectionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/BasicPermissionCollectionTest.java Thu Jun 1 01:15:17 2006
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2005 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.
+ */
+
+/**
+* @author Alexey V. Varlamov
+* @version $Revision$
+*/
+
+package java.security;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Enumeration;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests for <code>BasicPermissionCollection</code>
+ *
+ */
+
+public class BasicPermissionCollectionTest extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(BasicPermissionCollectionTest.class);
+ }
+
+ /**
+ * Can add only BasicPermissions of the same type as the first added. Cannot
+ * add if collection is read-only.
+ */
+ public void testAdd() {
+ PermissionCollection pc = new BasicPermissionCollection();
+ Permission ap = new AllPermission();
+ Permission sp1 = new SecurityPermission("a.b.c");
+ Permission sp2 = new SecurityPermission("a.b.*");
+ try {
+ pc.add(ap);
+ fail("Should not add non-BasicPermission");
+ } catch (IllegalArgumentException ok) {
+ }
+ pc.add(sp1);
+ pc.add(sp2);
+ try {
+ pc.add(new BasicPermission("123") {
+ });
+ fail("Should not add BasicPermission of different type");
+ } catch (IllegalArgumentException ok) {
+ }
+
+ pc.setReadOnly();
+ try {
+ pc.add(sp1);
+ fail("read-only flag is ignored");
+ } catch (SecurityException ok) {
+ }
+ }
+
+ /**
+ * Empty collection implies nothing. Non-empty collection should imply all
+ * contained permissions, and should consider contained wildcards (if any).
+ */
+ public void testImplies() {
+ PermissionCollection pc = new BasicPermissionCollection();
+ Permission ap = new AllPermission();
+ Permission up = new UnresolvedPermission("safds", null, null, null);
+ Permission sp1 = new SecurityPermission("a.b.c");
+ Permission sp11 = new SecurityPermission("a.b.");
+ Permission sp2 = new SecurityPermission("a.b.*");
+ Permission sp3 = new SecurityPermission("a.*");
+ Permission sp4 = new SecurityPermission("*");
+
+ assertFalse(pc.implies(ap));
+ assertFalse(pc.implies(up));
+ assertFalse(pc.implies(sp1));
+
+ pc.add(sp3);
+ assertTrue(pc.implies(sp2));
+ assertTrue(pc.implies(sp1));
+ assertTrue(pc.implies(sp11));
+ assertTrue(pc.implies(sp3));
+ assertFalse(pc.implies(sp4));
+
+ pc.add(sp4);
+ assertTrue(pc.implies(sp4));
+ assertFalse(pc.implies(ap));
+ assertFalse(pc.implies(up));
+ assertTrue(pc.implies(new SecurityPermission("skjdnkwje wefkwjef")));
+ }
+
+ /**
+ * Should return non-null empty enumeration for empty collection. For
+ * non-empty collection, should always return enumeration over unique
+ * elements.
+ */
+ public void testElements() {
+ PermissionCollection pc = new BasicPermissionCollection();
+ Permission sp1 = new SecurityPermission("a.b.c");
+ Permission sp2 = new SecurityPermission("a.b.*");
+ Permission sp3 = new SecurityPermission("*");
+ Enumeration en = pc.elements();
+ assertNotNull(en);
+ assertFalse(en.hasMoreElements());
+
+ try {
+ pc.add(null);
+ fail("should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+
+ pc.add(sp1);
+ en = pc.elements();
+ assertTrue(en.hasMoreElements());
+ assertTrue(sp1.equals(en.nextElement()));
+ assertFalse(en.hasMoreElements());
+
+ pc.add(sp1);
+ en = pc.elements();
+ assertTrue(en.hasMoreElements());
+ assertTrue(sp1.equals(en.nextElement()));
+ assertFalse(en.hasMoreElements());
+
+ pc.add(sp3);
+ pc.add(sp2);
+ en = pc.elements();
+ Collection els = new ArrayList();
+ while (en.hasMoreElements()) {
+ els.add(en.nextElement());
+ }
+ assertEquals(3, els.size());
+ assertTrue(els.containsAll(Arrays.asList(new Permission[] {
+ sp1, sp2, sp3 })));
+ }
+
+ /**
+ * test on deserialization of incorrect object
+ */
+ public void testNegDeserialization_01() throws Exception {
+
+ SecurityPermission sp = new SecurityPermission("a.b.c");
+ BasicPermissionCollection pc = new BasicPermissionCollection();
+ pc.add(sp);
+ setField(pc, "permClass", BasicPermission.class);
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(pc);
+ oos.flush();
+
+ ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
+ baos.toByteArray()));
+ try {
+ in.readObject();
+ fail("should throw InvalidObjectException");
+ } catch (java.io.InvalidObjectException e) {
+ } finally {
+ oos.close();
+ in.close();
+ }
+ }
+
+ /**
+ * test on deserialization of incorrect object
+ */
+ public void testNegDeserialization_02() throws Exception {
+
+ SecurityPermission sp = new SecurityPermission("a.b.c");
+ BasicPermissionCollection pc = new BasicPermissionCollection();
+ pc.add(sp);
+ setField(pc, "allEnabled", new Boolean(true));
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ObjectOutputStream oos = new ObjectOutputStream(baos);
+ oos.writeObject(pc);
+ oos.flush();
+
+ ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
+ baos.toByteArray()));
+ try {
+ in.readObject();
+ fail("should throw InvalidObjectException");
+ } catch (java.io.InvalidObjectException e) {
+ } finally {
+ oos.close();
+ in.close();
+ }
+ }
+
+ /**
+ * setup a private field
+ */
+ private void setField(Object obj, String name, Object newval)
+ throws Exception {
+ Field f = obj.getClass().getDeclaredField(name);
+ f.setAccessible(true);
+ f.set(obj, newval);
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/BasicPermissionTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/BasicPermissionTest.java?rev=410788&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/BasicPermissionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/BasicPermissionTest.java Thu Jun 1 01:15:17 2006
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2005 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.
+ */
+
+/**
+* @author Alexey V. Varlamov
+* @version $Revision$
+*/
+
+package java.security;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for <code>BasicPermission</code>
+ *
+ */
+
+public class BasicPermissionTest extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(BasicPermissionTest.class);
+ }
+
+ /**
+ * Constructor for BasicPermissionTest.
+ * @param arg0
+ */
+ public BasicPermissionTest(String arg0) {
+ super(arg0);
+ }
+
+ /**
+ * Check all constructors: an object is created with the specified valid name.
+ * If name equal null then NPE should be thrown.
+ * If name is empty then IAE should be thrown.
+ * Action is ignored.
+ */
+ public void testCtor()
+ {
+ String name = "basic123*$%#";
+ BasicPermission test = new BasicPermission(name){};
+ assertEquals(name, test.getName());
+ assertEquals("", test.getActions());
+ test = new BasicPermission(name, "#$!#12435"){};
+ assertEquals(name, test.getName());
+ assertEquals("", test.getActions());
+ try{
+ new BasicPermission(null){};
+ fail("NPE is not thrown");
+ }
+ catch (NullPointerException ok){}
+
+ try{
+ new BasicPermission(null, "ds235"){};
+ fail("NPE is not thrown");
+ }
+ catch (NullPointerException ok){}
+
+ try{
+ new BasicPermission(""){};
+ fail("IAE is not thrown");
+ }
+ catch (IllegalArgumentException ok){}
+ try{
+ new BasicPermission("", "ertre 3454"){};
+ fail("IAE is not thrown");
+ }
+ catch (IllegalArgumentException ok){}
+ }
+
+ private final class BasicPermissionImpl extends BasicPermission
+ {
+ public BasicPermissionImpl(String name)
+ {
+ super(name);
+ }
+ }
+
+ /**
+ * two BasicPermissions are equal if name and class are equal;
+ * equal permissions should have the same hash code
+ */
+ public void testEquals()
+ {
+ BasicPermission b1 = new BasicPermissionImpl("abc");
+ BasicPermission b2 = null;
+ assertTrue(b1.equals(b1));
+ assertFalse(b1.equals(null));
+ assertFalse(b1.equals(new Object()));
+ assertFalse(b1.equals("abc"));
+ assertTrue(b1.equals(b2 = new BasicPermissionImpl("abc")));
+ assertTrue(b1.hashCode() == b2.hashCode());
+ assertFalse(b1.equals(new BasicPermission("abc"){}));
+ assertFalse(b1.equals(new BasicPermissionImpl("abc.*")));
+ }
+
+ /**
+ * implies() should return true if a permission is equal to or is implied
+ * by wildcarded permission, false otherwise.
+ */
+ public void testImplies()
+ {
+ BasicPermission b1 = new BasicPermissionImpl("a.b.c");
+ assertTrue(b1.implies(b1));
+ assertTrue(b1.implies(new BasicPermissionImpl("a.b.c")));
+ assertFalse(b1.implies(new BasicPermissionImpl("a.b.c.*")));
+ assertFalse(b1.implies(new BasicPermission("a.b.c"){}));
+ assertTrue(new BasicPermissionImpl("a.b.*").implies(b1));
+ assertTrue(new BasicPermissionImpl("a.*").implies(b1));
+ assertTrue(new BasicPermissionImpl("*").implies(b1));
+ assertFalse(new BasicPermissionImpl("a.b*").implies(b1));
+ assertFalse(new BasicPermissionImpl("a.b.c.*").implies(b1));
+ assertTrue(new BasicPermissionImpl("1.*").implies(new BasicPermissionImpl("1.234.*")));
+ assertTrue(new BasicPermissionImpl("*").implies(new BasicPermissionImpl("*")));
+ }
+
+ /**
+ * newPermissionCollection() should return new BasicPermissionCollection on every invokation
+ */
+ public void testCollection()
+ {
+ BasicPermission b1 = new BasicPermissionImpl("a.b.c");
+ PermissionCollection pc1 = b1.newPermissionCollection();
+ PermissionCollection pc2 = b1.newPermissionCollection();
+ assertTrue((pc1 instanceof BasicPermissionCollection) && (pc2 instanceof BasicPermissionCollection));
+ assertNotSame(pc1, pc2);
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/ProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/ProviderTest.java?rev=410788&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/ProviderTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/ProviderTest.java Thu Jun 1 01:15:17 2006
@@ -0,0 +1,535 @@
+/*
+ * Copyright 2005 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.
+ */
+
+/**
+* @author Boris V. Kuznetsov
+* @version $Revision$
+*/
+
+package java.security;
+
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import junit.framework.TestCase;
+
+import org.apache.harmony.security.tests.support.SpiEngUtils;
+import org.apache.harmony.security.tests.support.TestUtils;
+
+
+/**
+ * Tests for <code>Provider</code> constructor and methods
+ *
+ */
+public class ProviderTest extends TestCase {
+
+ Provider p;
+
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ p = new MyProvider();
+ }
+
+ /*
+ * Class under test for void Provider()
+ */
+ public final void testProvider() {
+ if (!p.getProperty("Provider.id name").equals(String.valueOf(p.getName()))) {
+ fail("Incorrect \"Provider.id name\" value");
+ }
+ if (!p.getProperty("Provider.id version").equals(String.valueOf(p.getVersion()))) {
+ fail("Incorrect \"Provider.id version\" value");
+ }
+ if (!p.getProperty("Provider.id info").equals(String.valueOf(p.getInfo()))) {
+ fail("Incorrect \"Provider.id info\" value");
+ }
+ if (!p.getProperty("Provider.id className").equals(p.getClass().getName())) {
+ fail("Incorrect \"Provider.id className\" value");
+ }
+ }
+
+ public final void testClear() {
+ p.clear();
+ if (p.getProperty("MessageDigest.SHA-1") != null) {
+ fail("Provider contains properties");
+ }
+ }
+
+ /*
+ * Class under test for void Provider(String, double, String)
+ */
+ public final void testProviderStringdoubleString() {
+ Provider p = new MyProvider("Provider name", 123.456, "Provider info");
+ if (!p.getName().equals("Provider name") ||
+ p.getVersion() != 123.456 ||
+ !p.getInfo().equals("Provider info")) {
+ fail("Incorrect values");
+ }
+ }
+
+ public final void testGetName() {
+ if (!p.getName().equals("MyProvider")) {
+ fail("Incorrect provider name");
+ }
+ }
+
+ public final void testGetVersion() {
+ if (p.getVersion() != 1.0) {
+ fail("Incorrect provider version");
+ }
+ }
+
+ public final void testGetInfo() {
+ if (!p.getInfo().equals("Provider for testing")) {
+ fail("Incorrect provider info");
+ }
+ }
+
+ /*
+ * Class under test for String toString()
+ */
+ public final void testToString() {
+ if (!"MyProvider provider, Ver. 1.0 Provider for testing".equals(p.toString())) {
+ fail("Incorrect provider.toString()");
+ }
+ }
+
+ /*
+ * Class under test for void load(InputStream)
+ */
+ public final void testLoadInputStream() {
+ FileInputStream fis = null;
+ String fileName = SpiEngUtils.getFileName(TestUtils.TEST_ROOT,
+ "java/security/Provider.prop.dat");
+ try {
+ fis = new FileInputStream(fileName);
+ } catch (FileNotFoundException e) {
+ fail(e.toString());
+ }
+ try {
+ p.load(fis);
+ } catch (IOException e) {
+ fail(e.toString());
+ }
+
+ if (!"value 1".equals(p.getProperty("Property 1").trim()) ||
+ !"className".equals(p.getProperty("serviceName.algName").trim()) ||
+ !"attrValue".equals(p.getProperty("serviceName.algName attrName").trim()) ||
+ !"stanbdardName".equals(p.getProperty("Alg.Alias.engineClassName.aliasName").trim()) ||
+ !String.valueOf(p.getName()).equals(p.getProperty("Provider.id name").trim()) ||
+ !String.valueOf(p.getVersion()).equals(p.getProperty("Provider.id version").trim()) ||
+ !String.valueOf(p.getInfo()).equals(p.getProperty("Provider.id info").trim()) ||
+ !p.getClass().getName().equals(p.getProperty("Provider.id className").trim()) ||
+ !"SomeClassName".equals(p.getProperty("MessageDigest.SHA-1").trim()) ) {
+ fail("Incorrect property value");
+ }
+ }
+
+ /*
+ * Class under test for void putAll(Map)
+ */
+ public final void testPutAllMap() {
+ HashMap hm = new HashMap();
+ hm.put("MessageDigest.SHA-1", "aaa.bbb.ccc.ddd");
+ hm.put("Property 1", "value 1");
+ hm.put("serviceName.algName attrName", "attrValue");
+ hm.put("Alg.Alias.engineClassName.aliasName", "stanbdardName");
+ p.putAll(hm);
+ if (!"value 1".equals(p.getProperty("Property 1").trim()) ||
+ !"attrValue".equals(p.getProperty("serviceName.algName attrName").trim()) ||
+ !"stanbdardName".equals(p.getProperty("Alg.Alias.engineClassName.aliasName").trim()) ||
+ !"aaa.bbb.ccc.ddd".equals(p.getProperty("MessageDigest.SHA-1").trim()) ) {
+ fail("Incorrect property value");
+ }
+ }
+
+ /*
+ * Class under test for Set entrySet()
+ */
+ public final void testEntrySet() {
+ p.put("MessageDigest.SHA-256", "aaa.bbb.ccc.ddd");
+
+ Set s = p.entrySet();
+ try {
+ s.clear();
+ } catch (UnsupportedOperationException e) {
+ }
+ Set s1 = p.entrySet();
+ if ((s == s1) || s1.isEmpty() ) {
+ fail("Must return unmodifiable set");
+ }
+ if (s1.size() != 8) {
+ fail("Incorrect set size");
+ }
+ for (Iterator it = s1.iterator(); it.hasNext();) {
+ Entry e = (Entry)it.next();
+ String key = (String)e.getKey();
+ String val = (String)e.getValue();
+ if (key.equals("MessageDigest.SHA-1") && val.equals("SomeClassName")) {
+ continue;
+ }
+ if (key.equals("Alg.Alias.MessageDigest.SHA1") && val.equals("SHA-1")) {
+ continue;
+ }
+ if (key.equals("MessageDigest.abc") && val.equals("SomeClassName")) {
+ continue;
+ }
+ if (key.equals("Provider.id className") && val.equals(p.getClass().getName())) {
+ continue;
+ }
+ if (key.equals("Provider.id name") && val.equals("MyProvider")) {
+ continue;
+ }
+ if (key.equals("MessageDigest.SHA-256") && val.equals("aaa.bbb.ccc.ddd")) {
+ continue;
+ }
+ if (key.equals("Provider.id version") && val.equals("1.0")) {
+ continue;
+ }
+ if (key.equals("Provider.id info") && val.equals("Provider for testing")) {
+ continue;
+ }
+ fail("Incorrect set");
+ }
+ }
+
+ /*
+ * Class under test for Set keySet()
+ */
+ public final void testKeySet() {
+ p.put("MessageDigest.SHA-256", "aaa.bbb.ccc.ddd");
+
+ Set s = p.keySet();
+ try {
+ s.clear();
+ } catch (UnsupportedOperationException e) {
+ }
+ Set s1 = p.keySet();
+ if ((s == s1) || s1.isEmpty() ) {
+ fail("Must return unmodifiable set");
+ }
+ if (s1.size() != 8) {
+ fail("Incorrect set size");
+ }
+ if (!s1.contains("MessageDigest.SHA-256") ||
+ !s1.contains("MessageDigest.SHA-1") ||
+ !s1.contains("Alg.Alias.MessageDigest.SHA1") ||
+ !s1.contains("MessageDigest.abc") ||
+ !s1.contains("Provider.id info") ||
+ !s1.contains("Provider.id className") ||
+ !s1.contains("Provider.id version") ||
+ !s1.contains("Provider.id name")) {
+ fail("Incorrect set");
+ }
+ }
+
+ /*
+ * Class under test for Collection values()
+ */
+ public final void testValues() {
+ p.put("MessageDigest.SHA-256", "aaa.bbb.ccc.ddd");
+
+ Collection c = p.values();
+ try {
+ c.clear();
+ } catch (UnsupportedOperationException e) {
+ }
+ Collection c1 = p.values();
+ if ((c == c1) || c1.isEmpty() ) {
+ fail("Must return unmodifiable set");
+ }
+ if (c1.size() != 8) {
+ fail("Incorrect set size " + c1.size());
+ }
+ if (!c1.contains("MyProvider") ||
+ !c1.contains("aaa.bbb.ccc.ddd") ||
+ !c1.contains("Provider for testing") ||
+ !c1.contains("1.0") ||
+ !c1.contains("SomeClassName") ||
+ !c1.contains("SHA-1") ||
+ !c1.contains(p.getClass().getName())) {
+ fail("Incorrect set");
+ }
+ }
+
+ /*
+ * Class under test for Object put(Object, Object)
+ */
+ public final void testPutObjectObject() {
+ p.put("MessageDigest.SHA-1", "aaa.bbb.ccc.ddd");
+ p.put("Type.Algorithm", "className");
+ if (!"aaa.bbb.ccc.ddd".equals(p.getProperty("MessageDigest.SHA-1").trim()) ) {
+ fail("Incorrect property value");
+ }
+
+ Set services = p.getServices();
+ if (services.size() != 3) {
+ fail("incorrect size");
+ }
+ for (Iterator it = services.iterator(); it.hasNext();) {
+ Provider.Service s = (Provider.Service)it.next();
+ if ("Type".equals(s.getType()) &&
+ "Algorithm".equals(s.getAlgorithm()) &&
+ "className".equals(s.getClassName())) {
+ continue;
+ }
+ if ("MessageDigest".equals(s.getType()) &&
+ "SHA-1".equals(s.getAlgorithm()) &&
+ "aaa.bbb.ccc.ddd".equals(s.getClassName())) {
+ continue;
+ }
+ if ("MessageDigest".equals(s.getType()) &&
+ "abc".equals(s.getAlgorithm()) &&
+ "SomeClassName".equals(s.getClassName())) {
+ continue;
+ }
+ fail("Incorrect service");
+ }
+ }
+
+ /*
+ * Class under test for Object remove(Object)
+ */
+ public final void testRemoveObject() {
+ Object o = p.remove("MessageDigest.SHA-1");
+ if (!"SomeClassName".equals(o)) {
+ fail("Incorrect return value");
+ }
+ if (p.getProperty("MessageDigest.SHA-1") != null) {
+ fail("Provider contains properties");
+ }
+ if (p.getServices().size() != 1){
+ fail("Service not removed");
+ }
+ }
+
+ public final void testImplementsAlg() {
+ HashMap hm = new HashMap();
+ hm.put("KeySize", "1024");
+ hm.put("AAA", "BBB");
+ Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
+ "className", null, hm);
+ p.putService(s);
+ if (!p.implementsAlg("Type", "Algorithm", null, null) ||
+ !p.implementsAlg("MessageDigest", "SHA-1", null, null)) {
+ fail("Case 1. implementsAlg failed");
+ }
+ if (!p.implementsAlg("Type", "Algorithm", "KeySize", "512")) {
+ fail("Case 2. implementsAlg failed");
+ }
+ if (p.implementsAlg("Type", "Algorithm", "KeySize", "1025")) {
+ fail("Case 3. implementsAlg failed");
+ }
+ if (!p.implementsAlg("Type", "Algorithm", "AAA", "BBB")) {
+ fail("Case 3. implementsAlg failed");
+ }
+ }
+
+ public final void testSetProviderNumber() {
+ p.setProviderNumber(100);
+ if (p.getProviderNumber() != 100) {
+ fail("Incorrect ProviderNumber");
+ }
+ }
+
+ public final void testGetProviderNumber() {
+ if (p.getProviderNumber() != -1) {
+ fail("Case 1. Incorrect ProviderNumber");
+ }
+
+ int i = Security.addProvider(p);
+ if (p.getProviderNumber() != i) {
+ fail("Case 2. Incorrect ProviderNumber");
+ }
+ Security.removeProvider(p.getName()); // clean up
+ }
+
+ public final void testGetService() {
+ try {
+ p.getService(null, "algorithm");
+ fail("No expected NullPointerException");
+ } catch (NullPointerException e) {
+ }
+ try {
+ p.getService("type", null);
+ fail("No expected NullPointerException");
+ } catch (NullPointerException e) {
+ }
+
+ Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
+ "className", null, null);
+ p.putService(s);
+
+ if (p.getService("Type", "AlgoRithM") != s) {
+ fail("Case 1. getService() failed");
+ }
+
+ Provider.Service s1 = p.getService("MessageDigest", "AbC");
+ if (s1 == null) {
+ fail("Case 2. getService() failed");
+ }
+
+ s = new Provider.Service(p, "MessageDigest", "SHA-1",
+ "className", null, null);
+ p.putService(s);
+ if (s1 == p.getService("MessageDigest", "SHA-1")) {
+ fail("Case 3. getService() failed");
+ }
+
+ if (p.getService("MessageDigest", "SHA1") == null) {
+ fail("Case 4. getService() failed");
+ }
+ }
+
+ public final void testGetServices() {
+ Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
+ "className", null, null);
+
+ // incomplete services should be removed
+ p.put("serv.alg", "aaaaaaaaaaaaa");
+ p.put("serv.alg KeySize", "11111");
+ p.put("serv1.alg1 KeySize", "222222");
+ p.remove("serv.alg");
+
+ p.putService(s);
+ Set services = p.getServices();
+ if (services.size() != 3) {
+ fail("incorrect size");
+ }
+ for (Iterator it = services.iterator(); it.hasNext();) {
+ s = (Provider.Service)it.next();
+ if ("Type".equals(s.getType()) &&
+ "Algorithm".equals(s.getAlgorithm()) &&
+ "className".equals(s.getClassName())) {
+ continue;
+ }
+ if ("MessageDigest".equals(s.getType()) &&
+ "SHA-1".equals(s.getAlgorithm()) &&
+ "SomeClassName".equals(s.getClassName())) {
+ continue;
+ }
+ if ("MessageDigest".equals(s.getType()) &&
+ "abc".equals(s.getAlgorithm()) &&
+ "SomeClassName".equals(s.getClassName())) {
+ continue;
+ }
+ fail("Incorrect service");
+ }
+ }
+
+ public final void testPutService() {
+ HashMap hm = new HashMap();
+ hm.put("KeySize", "1024");
+ hm.put("AAA", "BBB");
+ Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
+ "className", null, hm);
+ p.putService(s);
+ if (s != p.getService("Type", "Algorithm")){
+ fail("putService failed");
+ }
+ if (!"className".equals(p.getProperty("Type.Algorithm"))) {
+ fail("incorrect className");
+ }
+ if (!"1024".equals(p.getProperty("Type.Algorithm KeySize"))) {
+ fail("incorrect attribute");
+ }
+ }
+
+ public final void testRemoveService() {
+ Provider.Service s = new Provider.Service(p, "Type", "Algorithm",
+ "className", null, null);
+ p.putService(s);
+ p.removeService(s);
+ Set services = p.getServices();
+ if (services.size() != 2) {
+ fail("incorrect size");
+ }
+
+ for (Iterator it = services.iterator(); it.hasNext();) {
+ s = (Provider.Service)it.next();
+ if ("MessageDigest".equals(s.getType()) &&
+ "SHA-1".equals(s.getAlgorithm()) &&
+ "SomeClassName".equals(s.getClassName())) {
+ continue;
+ }
+ if ("MessageDigest".equals(s.getType()) &&
+ "abc".equals(s.getAlgorithm()) &&
+ "SomeClassName".equals(s.getClassName())) {
+ continue;
+ }
+ fail("Incorrect service");
+ }
+
+ if (p.getProperty("Type.Algorithm") != null) {
+ fail("incorrect property");
+ }
+ }
+
+ public final void testService1() {
+ p.put("MessageDigest.SHA-1", "AnotherClassName");
+ Provider.Service s = p.getService("MessageDigest", "SHA-1");
+ if (!"AnotherClassName".equals(s.getClassName())) {
+ fail("Incorrect class name "+ s.getClassName());
+ }
+ }
+
+ /*
+ public final void testService2() {
+ Provider[] pp = Security.getProviders("MessageDigest.SHA-1");
+ if (pp == null) {
+ return;
+ }
+ Provider p2 = pp[0];
+ String old = p2.getProperty("MessageDigest.SHA-1");
+ try {
+ p2.put("MessageDigest.SHA-1", "AnotherClassName");
+ Provider.Service s = p2.getService("MessageDigest", "SHA-1");
+ if (!"AnotherClassName".equals(s.getClassName())) {
+ fail("Incorrect class name "+ s.getClassName());
+ }
+ try {
+ s.newInstance(null);
+ fail("No expected NoSuchAlgorithmException");
+ } catch (NoSuchAlgorithmException e) {
+ }
+ } finally {
+ p2.put("MessageDigest.SHA-1", old);
+ }
+ }
+*/
+
+ class MyProvider extends Provider {
+ MyProvider() {
+ super("MyProvider", 1.0, "Provider for testing");
+ put("MessageDigest.SHA-1", "SomeClassName");
+ put("MessageDigest.abc", "SomeClassName");
+ put("Alg.Alias.MessageDigest.SHA1", "SHA-1");
+ }
+
+ MyProvider(String name, double version, String info) {
+ super(name, version, info);
+ }
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/SecurityTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/SecurityTest.java?rev=410788&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/SecurityTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/SecurityTest.java Thu Jun 1 01:15:17 2006
@@ -0,0 +1,442 @@
+/*
+ * Copyright 2005 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.
+ */
+
+/**
+* @author Boris V. Kuznetsov
+* @version $Revision$
+*/
+
+package java.security;
+
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.harmony.security.tests.support.TestKeyPair;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests for <code>Security</code> constructor and methods
+ *
+ */
+public class SecurityTest extends TestCase {
+
+ public final void testMixed() {
+
+ TestKeyPair tkp = null;
+ try {
+ tkp = new TestKeyPair("DSA");
+ } catch (NoSuchAlgorithmException e1) {
+ e1.printStackTrace();
+ return;
+ }
+
+ try {
+ MessageDigest.getInstance("SHA-1");
+ KeyFactory.getInstance("DSA");
+ Signature ss =Signature.getInstance("DSA");
+ ss.initSign(tkp.getPrivate());
+ Signature.getInstance("aaaaaaaaaaaa");
+ } catch (Exception e) {
+ // ignore
+ }
+
+ }
+
+ public final void testGetAlgorithmProperty() {
+ if (Security.getAlgorithmProperty(null, "MyService") != null ||
+ Security.getAlgorithmProperty("MyAlgorithm", null) != null) {
+ fail("Incorrect result on null parameter");
+ }
+ if (Security.getAlgorithmProperty("MyAlgorithm", "MyService") != null) {
+ fail("Incorrect result (provider not added)");
+ }
+ Provider p = new MyProvider();
+ Security.addProvider(p);
+ try {
+ if (!"SomeClassName".equals(Security.getAlgorithmProperty("MyAlGoriThm", "MySerVicE"))) {
+ fail("Incorrect result (provider added)");
+ }
+ } finally { //clean up
+ Security.removeProvider(p.getName());
+ }
+ }
+
+ public final void testInsertProviderAt() {
+ Provider p = new MyProvider();
+ int position;
+ int newposition;
+ Provider providers[] = Security.getProviders();
+ int providersNumber = providers.length;
+
+ try {
+
+ // Insert at position -1
+ position = -1;
+ newposition = Security.insertProviderAt(p, position);
+ if (newposition != (providersNumber + 1)) {
+ fail("Case 1. Newposition is " + newposition + ", should be " + (providersNumber + 1));
+ }
+ providers = Security.getProviders();
+ if (providers[newposition-1] != p) {
+ fail("Case 1. Provider not inserted at position " + newposition);
+ }
+
+ // A provider cannot be added if it is already installed
+ newposition = Security.insertProviderAt(p, 1);
+ if (newposition != -1) {
+ fail("Case 2. Newposition is " + newposition + ", should be -1");
+ }
+
+ Security.removeProvider(p.getName());
+
+ // insert at the end
+ position = providersNumber + 100;
+ newposition = Security.insertProviderAt(p, position);
+ if (newposition != (providersNumber + 1)) {
+ fail("Case 3. Newposition is " + newposition + ", should be " + (providersNumber + 1));
+ }
+ providers = Security.getProviders();
+ if (providers[newposition-1] != p) {
+ fail("Case 3. Provider not inserted at position " + newposition);
+ }
+
+ Security.removeProvider(p.getName());
+
+ // insert at the first position
+ position = 1;
+ newposition = Security.insertProviderAt(p, position);
+ if (newposition != position) {
+ fail("Case 4. Newposition is " + newposition + ", should be " + position);
+ }
+ providers = Security.getProviders();
+ if (providers[newposition-1] != p) {
+ fail("Case 4. Provider not inserted at position " + newposition);
+ }
+
+ try {
+ Security.insertProviderAt(null, position);
+ fail("Case 5. No expected NullPointerException.");
+ } catch (NullPointerException e) {
+ }
+ } finally { //clean up
+ Security.removeProvider(p.getName());
+ }
+ }
+
+ public final void testAddProvider() {
+
+ Provider p = new MyProvider();
+ int newposition;
+ Provider providers[] = Security.getProviders();
+ int providersNumber = providers.length;
+
+ try {
+ // add
+ newposition = Security.addProvider(p);
+ if (newposition != (providersNumber + 1)) {
+ fail("Case 1. Newposition is " + newposition + ", should be " + (providersNumber + 1));
+ }
+ providers = Security.getProviders();
+ if (providers[newposition-1] != p) {
+ fail("Case 1. Provider not inserted at position " + newposition);
+ }
+
+ // A provider cannot be added if it is already installed
+ newposition = Security.addProvider(p);
+ if (newposition != -1) {
+ fail("Case 2. Newposition is " + newposition + ", should be -1");
+ }
+
+ try {
+ Security.addProvider(null);
+ fail("Case 3. No expected NullPointerException.");
+ } catch (NullPointerException e) {
+ }
+ } finally { //clean up
+ Security.removeProvider(p.getName());
+ }
+
+ }
+
+ public final void testRemoveProvider() {
+ Provider[] providers;
+ Provider[] providers1;
+
+ providers = Security.getProviders();
+
+ try {
+ for (int i = 0; i < providers.length; i++) {
+ Security.removeProvider(providers[i].getName());
+ }
+ if (Security.getProviders().length != 0) {
+ fail("Providers not removed");
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ } finally { // restore providers
+ for (int i = 0; i < providers.length; i++) {
+ Security.addProvider(providers[i]);
+ }
+ providers1 = Security.getProviders();
+ for (int i = 0; i < providers1.length; i++) {
+ if (providers[i] != providers1[i]) {
+ fail("Providers not restored correctly");
+ }
+ }
+ }
+ }
+
+ /*
+ * Class under test for Provider[] getProviders()
+ */
+ public final void testGetProviders() {
+ Provider[] providers;
+
+ providers = Security.getProviders();
+ for (int i = 0; i < providers.length; i++) {
+ if (providers[i].getProviderNumber() != i+1) { // position is 1-based
+ fail("Incorrect provider number");
+ }
+ }
+ }
+
+ /*
+ * Class under test for Provider getProvider(String)
+ */
+ public final void testGetProvider() {
+ Provider p = new MyProvider();
+ Provider p1;
+
+ try {
+ Security.addProvider(p);
+
+ p1 = Security.getProvider(p.getName());
+ if (p1 != p) {
+ fail("Case 1. Incorrect provider is returned");
+ }
+
+ // Returns null if no provider with the specified name is installed
+ p1 = Security.getProvider("SOMEINCORRECTPROVIDERNAME");
+ if (p1 != null) {
+ fail("Case 2. Incorrect provider is returned");
+ }
+
+ // Returns null if name is null
+ p1 = Security.getProvider(null);
+ if (p1 != null) {
+ fail("Case 2. Incorrect provider is returned");
+ }
+ } finally { //clean up
+ Security.removeProvider(p.getName());
+ }
+ }
+
+ /*
+ * Class under test for Provider[] getProviders(String)
+ */
+ public final void testGetProvidersString() {
+ Provider[] providers;
+ try {
+ Security.getProviders("");
+ fail("No expected InvalidParameterException");
+ } catch (InvalidParameterException e) {
+ }
+
+
+ try {
+ Security.getProviders((String)null);
+ fail("No expected NullPointerException");
+ } catch (NullPointerException e) {
+ }
+
+ try {
+ Security.getProviders("AAA.BBB CCC");
+ fail("AAA.BBB CCC: No expected InvalidParameterException");
+ } catch (InvalidParameterException e) {
+ }
+
+ Provider p = new MyProvider();
+
+ try {
+ Security.addProvider(p);
+ providers = Security.getProviders("MyService.MyAlgorithm");
+ if (providers == null ||
+ providers.length != 1 ||
+ providers[0] != p) {
+ fail("fail for MyService.MyAlgorithm");
+ }
+
+ providers = Security.getProviders("MyService.MyAlgorithm KeySize:512");
+ if (providers == null ||
+ providers.length != 1 ||
+ providers[0] != p) {
+ fail("fail for MyService.MyAlgorithm KeySize:512");
+ }
+
+ providers = Security.getProviders("MyService.MyAlgorithm KeySize:1025");
+ if (providers != null) {
+ fail("fail for MyService.MyAlgorithm KeySize:1025");
+ }
+ } finally { //clean up
+ Security.removeProvider(p.getName());
+ }
+ }
+
+ /*
+ * Class under test for Provider[] getProviders(Map)
+ */
+ public final void testGetProvidersMap() {
+ Provider[] providers;
+ Map m = new Properties();
+ Security.getProviders(m);
+ if (Security.getProviders(m) != null) {
+ fail("Not null result on empty map");
+ }
+
+ try {
+ Security.getProviders((Map)null);
+ fail("No expected NullPointerException");
+ } catch (NullPointerException e) {
+ }
+
+ m.clear();
+ m.put("AAA.BBB CCC", "");
+ m.put("AAA.BBB", "");
+ try {
+ Security.getProviders(m);
+ fail("attribute value is empty string: No expected InvalidParameterException");
+ } catch (InvalidParameterException e) {
+ }
+
+ m.clear();
+ m.put("AAA.BBB.CCC", "aaaa");
+ m.put("AAA.BBB", "");
+ try {
+ Security.getProviders(m);
+ fail("value associated with the key is not an empty string: No expected InvalidParameterException");
+ } catch (InvalidParameterException e) {
+ }
+
+ Provider p = new MyProvider();
+ try {
+ Security.addProvider(p);
+ m.clear();
+ m.put("MyService.MyAlgorithm", "");
+ m.put("MessageDigest.SHA-1", "");
+ providers = Security.getProviders(m);
+ if (providers == null ||
+ providers.length != 1 ||
+ providers[0] != p) {
+ fail("fail for MyService.MyAlgorithm");
+ }
+
+ m.clear();
+ m.put("MyService.MyAlgorithm KeySize", "512");
+ m.put("MessageDigest.SHA-1", "");
+ providers = Security.getProviders(m);
+ if (providers == null ||
+ providers.length != 1 ||
+ providers[0] != p) {
+ fail("fail for MyService.MyAlgorithm KeySize:512");
+ }
+
+ m.clear();
+ m.put("MyService.MyAlgorithm KeySize", "1025");
+ m.put("MessageDigest.SHA-1", "");
+ providers = Security.getProviders(m);
+ if (providers != null) {
+ fail("fail for MyService.MyAlgorithm KeySize:1025");
+ }
+ } finally { //clean up
+ Security.removeProvider(p.getName());
+ }
+ }
+
+ public final void testSetGetProperty() {
+ try {
+ Security.getProperty(null);
+ fail("Case 1. No expected NullPointerException.");
+ } catch (NullPointerException e) {
+ }
+
+ try {
+ Security.setProperty(null, "");
+ fail("Case 2. No expected NullPointerException.");
+ } catch (NullPointerException e) {
+ }
+
+ try {
+ Security.setProperty("", null);
+ fail("Case 3. No expected NullPointerException.");
+ } catch (NullPointerException e) {
+ }
+
+ Security.setProperty("", "");
+ if (!"".equals(Security.getProperty("")) ) {
+ fail("Case 4. Empty property test failed");
+ }
+
+ Security.setProperty("My Test Property", "My property value");
+ if (!"My property value".equals(Security.getProperty("My Test Property")) ) {
+ fail("Case 5. Not empty property test failed");
+ }
+
+ }
+
+ public final void testGetAlgorithms() {
+ Set alg1;
+ Set alg2;
+
+ alg1 = Security.getAlgorithms("AAAAAAAAAAAAAAA");
+ if (alg1 == null || alg1.size() != 0) {
+ fail("fail for non-existent service");
+ }
+
+ alg1 = Security.getAlgorithms("SecureRandom");
+ alg2 = Security.getAlgorithms("seCuReranDom");
+ if (alg1.size() != alg2.size()) {
+ fail("different size");
+ }
+ if (!alg2.containsAll(alg1)) {
+ fail("different content");
+ }
+
+ Provider p = new MyProvider();
+
+ try {
+ Security.addProvider(p);
+ alg1 = Security.getAlgorithms("MyService");
+ if (alg1.size() != 1 || !alg1.contains("MyAlgorithm")) {
+ fail("fail for MyService");
+ }
+ } finally { //clean up
+ Security.removeProvider(p.getName());
+ }
+ }
+
+ class MyProvider extends Provider {
+ MyProvider() {
+ super("MyProvider", 1.0, "Provider for testing");
+ put("MessageDigest.SHA-1", "SomeClassName");
+ put("MyService.MyAlgorithm", "SomeClassName");
+ put("MyService.MyAlgorithm KeySize", "1024");
+ }
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermissionCollectionTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermissionCollectionTest.java?rev=410788&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermissionCollectionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermissionCollectionTest.java Thu Jun 1 01:15:17 2006
@@ -0,0 +1,201 @@
+/*
+ * Copyright 2005 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.
+ */
+
+/**
+* @author Alexey V. Varlamov
+* @version $Revision$
+*/
+
+package java.security;
+
+import java.util.*;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests for <code>UnresolvedPermissionCollection</code> class fields and methods
+ *
+ */
+
+public class UnresolvedPermissionCollectionTest extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(UnresolvedPermissionCollectionTest.class);
+ }
+
+ /**
+ * Can add any number of UnresolvedPermission instances, but no any other permissions.
+ * Cannot add if collection is read-only.
+ */
+ public void testAdd()
+ {
+ PermissionCollection pc = new UnresolvedPermissionCollection();
+ Permission sp = new SecurityPermission("abc");
+ Permission up1 = new UnresolvedPermission("131234", null, null, null);
+ Permission up2 = new UnresolvedPermission("KUJKHVKJgyuygjhb", "xcv456", "26r ytf",
+ new java.security.cert.Certificate[0]);
+
+ try {
+ pc.add(sp);
+ fail("Should not add non-UnresolvedPermission");
+ }
+ catch (IllegalArgumentException ok) {}
+
+ pc.add(up1);
+ pc.add(up1);
+ pc.add(up2);
+
+ pc.setReadOnly();
+ try {
+ pc.add(up1);
+ fail("read-only flag is ignored");
+ }
+ catch (SecurityException ok) {}
+ }
+
+ /** This collection never implies any permission. */
+ public void testImplies()
+ {
+ Permission ap = new AllPermission();
+ Permission up = new UnresolvedPermission("131234", null, null, null);
+ PermissionCollection pc = up.newPermissionCollection();
+
+ assertFalse(pc.implies(ap));
+ assertFalse(pc.implies(up));
+
+ //pc.add(up);
+ //assertFalse(pc.implies(up));
+ }
+
+ /**
+ * Should return non-null empty enumeration for empty collection.
+ * For non-empty collection, should always return enumeration over unique elements.
+ */
+ public void testElements()
+ {
+ PermissionCollection pc = new UnresolvedPermissionCollection();
+ Permission up1 = new UnresolvedPermission("131234", null, null, null);
+ Permission up2 = new UnresolvedPermission("131234", "ui23rjh", null, null);
+ Permission up3 = new UnresolvedPermission("KUJKHVKJgyuygjhb", "xcv456", "26r ytf",
+ new java.security.cert.Certificate[0]);
+
+ Enumeration en = pc.elements();
+ assertNotNull(en);
+ assertFalse(en.hasMoreElements());
+
+ pc.add(up1);
+ en = pc.elements();
+ assertTrue(en.hasMoreElements());
+ assertTrue(up1.equals(en.nextElement()));
+ assertFalse(en.hasMoreElements());
+
+ //no check for duplicate elements - this is too implementation specific.
+ /*pc.add(up1);
+ en = pc.elements();
+ assertTrue(en.hasMoreElements());
+ assertTrue(up1.equals(en.nextElement()));
+ assertFalse(en.hasMoreElements());*/
+
+ pc.add(up2);
+ pc.add(up3);
+ en = pc.elements();
+ Collection els = new ArrayList();
+ while (en.hasMoreElements())
+ {
+ els.add(en.nextElement());
+ }
+ assertEquals(3, els.size());
+ assertTrue(els.contains(up1) && els.contains(up2) && els.contains(up3));
+ }
+
+ /**
+ * For null collection passed, should behave correctly:
+ * <ul>
+ * <li>If nothing resolved, returns null and does not remove elements
+ * <li>If some permission resolved, returns proper collection and removes resolved elements
+ * </ul>
+ */
+ public void testResolveCollection()
+ {
+ UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
+ Permission up = new UnresolvedPermission("java.security.AllPermission", "xcv456", "26r ytf",
+ new java.security.cert.Certificate[0]);
+ Permission ap = new AllPermission();
+ Permission bp = new BasicPermission("sfwertsdg"){};
+
+ PermissionCollection resolved = upc.resolveCollection(ap, null);
+ assertNull(resolved);
+
+ upc.add(up);
+ resolved = upc.resolveCollection(bp, null);
+ assertNull(resolved);
+ assertTrue(up.equals(upc.elements().nextElement()));
+
+ resolved = upc.resolveCollection(ap, null);
+ assertNotNull(resolved);
+ assertTrue(ap.equals(resolved.elements().nextElement()));
+ assertFalse("resolved permission should be removed from unresolevd collection", upc.elements().hasMoreElements());
+ }
+
+ /**
+ * For real collection passed, should behave correctly:
+ * <ul>
+ * <li>If nothing resolved, returns the collection and does not remove elements
+ * <li>If some permission resolved, returns the collection and removes resolved elements
+ * </ul>
+ */
+ public void testResolveCollectionReturnedCollection()
+ {
+ UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
+ Permission up3 = new UnresolvedPermission("java.security.AllPermission", "xcv456", null, null);
+ Permission ap = new AllPermission();
+ PermissionCollection apc = new AllPermissionCollection();
+
+ PermissionCollection resolved = upc.resolveCollection(ap, apc);
+ assertSame("should return the passed collection if it is not null", apc, resolved);
+ // retest the same for case of actually resolved permission
+ upc.add(up3);
+ resolved = upc.resolveCollection(ap, apc);
+ assertSame("should return the passed collection if it is not null", apc, resolved);
+ }
+
+ /**
+ * Test for case when some permissions of the expected type were not resolved for some reason,
+ * while others were resolved. Returned collection should contain resolved permissions only,
+ * and the unresolved collection should retain unresolved ones only.
+ */
+ public void testResolveCollectionPartial()
+ {
+ UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
+ String name = "ui23rjh";
+ Permission up1 = new UnresolvedPermission("java.security.SecurityPermission", null, null, null);
+ Permission up2 = new UnresolvedPermission("java.security.SecurityPermission", name, null, null);
+ Permission sp = new SecurityPermission(name);
+
+ upc.add(up1);
+ upc.add(up2);
+ PermissionCollection resolved = upc.resolveCollection(new SecurityPermission("34po5ijh"), null);
+ assertNotNull(resolved);
+ Enumeration els = resolved.elements();
+ assertTrue(sp.equals(els.nextElement()));
+ assertFalse(els.hasMoreElements());
+ els = upc.elements();
+ assertTrue("resolved permission should be removed from unresolevd collection",
+ up1.equals(els.nextElement()));
+ assertFalse(els.hasMoreElements());
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermissionTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermissionTest.java?rev=410788&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermissionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java.injected/java/security/UnresolvedPermissionTest.java Thu Jun 1 01:15:17 2006
@@ -0,0 +1,175 @@
+/*
+ * Copyright 2005 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.
+ */
+
+/**
+* @author Alexey V. Varlamov
+* @version $Revision$
+*/
+
+package java.security;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for <code>UnresolvedPermission</code> class fields and methods
+ *
+ */
+
+public class UnresolvedPermissionTest extends TestCase {
+
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(UnresolvedPermissionTest.class);
+ }
+
+ /**
+ * Constructor for UnresolvedPermissionTest.
+ * @param arg0
+ */
+ public UnresolvedPermissionTest(String arg0) {
+ super(arg0);
+ }
+
+ /**
+ * Creates an Object with given name, type, action, certificaties.
+ * Empty or null type is not allowed - exception should be thrown.
+ */
+ public void testCtor()
+ {
+ String type = "laskjhlsdk 2345346";
+ String name = "^%#UHVKU^%V 887y";
+ String action = "JHB ^%(*&T klj3h4";
+ UnresolvedPermission up = new UnresolvedPermission(type, name, action, null);
+ assertEquals(type, up.getName());
+ assertEquals("", up.getActions());
+ assertEquals("(unresolved " + type + " " + name + " " + action + ")", up.toString());
+
+ up = new UnresolvedPermission(type, null, null, null);
+ assertEquals(type, up.getName());
+ assertEquals("", up.getActions());
+ assertEquals("(unresolved " + type + " null null)", up.toString());
+
+ up = new UnresolvedPermission(type, "", "", new java.security.cert.Certificate[0]);
+ assertEquals(type, up.getName());
+ assertEquals("", up.getActions());
+ assertEquals("(unresolved " + type + " )", up.toString());
+
+ try {
+ new UnresolvedPermission(null, name, action, null);
+ fail("exception is not thrown on null type");
+ }
+ catch (Exception ok) {}
+ /*try {
+ new UnresolvedPermission("", name, action, null);
+ fail("exception is not thrown on empty type");
+ }
+ catch (Exception ok) {}*/
+ }
+
+ /**
+ * This test is valid since 1.5 release only. Checks that UnresolvedPermission returns the proper
+ * data for target permission. For non-empty certificates array,
+ * returns a new array each time this method is called.
+ */
+ public void testTargetData()
+ {
+ String type = "laskjhlsdk 2345346";
+ String name = "^%#UHVKU^%V 887y";
+ String action = "JHB ^%(*&T klj3h4";
+ UnresolvedPermission up = new UnresolvedPermission(type, name, action, null);
+ assertEquals(type, up.getUnresolvedType());
+ 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()
+ {
+ String type = "KJHGUiy 24y";
+ String name = "kjhsdkfj ";
+ String action = "T klj3h4";
+ UnresolvedPermission up = new UnresolvedPermission(type, name, action, null);
+ UnresolvedPermission up2 = new UnresolvedPermission(type, name, action, null);
+ assertFalse(up.equals(null));
+ assertFalse(up.equals(new Object()));
+ assertFalse(up.equals(new BasicPermission("df"){}));
+ assertTrue(up.equals(up));
+ 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());
+ }
+
+ /**
+ * UnresolvedPermission never implies any other permission.
+ */
+ public void testImplies()
+ {
+ UnresolvedPermission up = new UnresolvedPermission("java.security.SecurityPermission", "a.b.c", null, null);
+ assertFalse(up.implies(up));
+ assertFalse(up.implies(new AllPermission()));
+ assertFalse(up.implies(new SecurityPermission("a.b.c")));
+ }
+
+ /**
+ * newPermissionCollection() should return new BasicPermissionCollection on every invokation
+ */
+ public void testCollection()
+ {
+ UnresolvedPermission up = new UnresolvedPermission("a.b.c", null, null, null);
+ PermissionCollection pc1 = up.newPermissionCollection();
+ PermissionCollection pc2 = up.newPermissionCollection();
+ assertTrue((pc1 instanceof UnresolvedPermissionCollection) && (pc2 instanceof UnresolvedPermissionCollection));
+ assertNotSame(pc1, pc2);
+ }
+
+ /**
+ * resolve the unresolved permission to the permission of specified class.
+ */
+ public void testResolve()
+ {
+ String name = "abc";
+ UnresolvedPermission up = new UnresolvedPermission("java.security.SecurityPermission", name, null, null);
+ Permission expected = new SecurityPermission(name);
+ //test valid input
+ assertEquals(expected, up.resolve(SecurityPermission.class));
+
+ //test invalid class
+ assertNull(up.resolve(Object.class));
+
+ //test invalid signers
+ //up = new UnresolvedPermission("java.security.SecurityPermission", name, null, new java.security.cert.Certificate[1]);
+ //assertNull(up.resolve(SecurityPermission.class));
+
+ //another valid case
+ up = new UnresolvedPermission("java.security.AllPermission", null, null, new java.security.cert.Certificate[0]);
+ assertEquals(new AllPermission(name, ""), up.resolve(AllPermission.class));
+ }
+}