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/13 10:58:17 UTC
svn commit: r413841 [7/9] - in
/incubator/harmony/enhanced/classlib/trunk/modules/security/src:
main/java/common/java/security/ test/api/java.injected/java/security/
test/api/java/org/apache/harmony/security/tests/java/security/
test/impl/java.injected...
Copied: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/MessageDigest_Impl2Test.java (from r412639, incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/MessageDigest2Test.java)
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/MessageDigest_Impl2Test.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/MessageDigest_Impl2Test.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/MessageDigest2Test.java&r1=412639&r2=413841&rev=413841&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/MessageDigest2Test.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/MessageDigest_Impl2Test.java Tue Jun 13 01:58:11 2006
@@ -19,7 +19,11 @@
* @version $Revision$
*/
-package java.security;
+package org.apache.harmony.security.tests.java.security;
+
+import java.security.MessageDigest;
+import java.security.Provider;
+import java.security.Security;
import org.apache.harmony.security.tests.support.MyMessageDigest1;
import org.apache.harmony.security.tests.support.MyMessageDigest2;
@@ -30,7 +34,7 @@
* Tests for <code>MessageDigest</code> constructor and methods
*
*/
-public class MessageDigest2Test extends TestCase {
+public class MessageDigest_Impl2Test extends TestCase {
/**
* Provider
@@ -57,177 +61,103 @@
/*
* Class under test for MessageDigest getInstance(String)
*/
- public void testGetInstanceString1() {
- MessageDigest md1 = null;
-
- try {
- md1 = MessageDigest.getInstance("ABC");
- } catch (NoSuchAlgorithmException e) {
- fail(e.toString());
- }
+ public void testGetInstanceString1() throws Exception {
+ MessageDigest md1 = MessageDigest.getInstance("ABC");
checkMD1(md1, p);
}
/*
* Class under test for MessageDigest getInstance(String)
*/
- public void testGetInstanceString2() {
- MessageDigest md2 = null;
-
- try {
- md2 = MessageDigest.getInstance("CBA");
- } catch (NoSuchAlgorithmException e) {
- fail(e.toString());
- }
+ public void testGetInstanceString2() throws Exception {
+ MessageDigest md2 = MessageDigest.getInstance("CBA");
checkMD2(md2, p);
}
/*
* Class under test for MessageDigest getInstance(String, String)
*/
- public void testGetInstanceStringString1() {
- MessageDigest md1 = null;
-
- try {
- md1 = MessageDigest.getInstance("ABC", "MyProvider");
- } catch (NoSuchAlgorithmException e) {
- fail(e.toString());
- } catch (NoSuchProviderException e) {
- fail(e.toString());
- }
+ public void testGetInstanceStringString1() throws Exception {
+ MessageDigest md1 = MessageDigest.getInstance("ABC", "MyProvider");
checkMD1(md1, p);
}
/*
* Class under test for MessageDigest getInstance(String, String)
*/
- public void testGetInstanceStringString2() {
- MessageDigest md2 = null;
- try {
- md2 = MessageDigest.getInstance("CBA", "MyProvider");
- } catch (NoSuchAlgorithmException e) {
- fail(e.toString());
- } catch (NoSuchProviderException e) {
- fail(e.toString());
- }
+ public void testGetInstanceStringString2() throws Exception {
+ MessageDigest md2 = MessageDigest.getInstance("CBA", "MyProvider");
checkMD2(md2, p);
}
/*
* Class under test for MessageDigest getInstance(String, Provider)
*/
- public void testGetInstanceStringProvider1() {
- MessageDigest md1 = null;
-
+ public void testGetInstanceStringProvider1() throws Exception {
Provider p = new MyProvider();
- try {
- md1 = MessageDigest.getInstance("ABC", p);
- } catch (NoSuchAlgorithmException e) {
- fail(e.toString());
- }
+ MessageDigest md1 = MessageDigest.getInstance("ABC", p);
checkMD1(md1, p);
}
/*
* Class under test for MessageDigest getInstance(String, Provider)
*/
- public void testGetInstanceStringProvider2() {
- MessageDigest md2 = null;
- Provider p = new MyProvider();
- try {
- md2 = MessageDigest.getInstance("CBA", p);
- } catch (NoSuchAlgorithmException e) {
- fail(e.toString());
- }
- checkMD2(md2, p);
- }
-
- private void checkMD1(MessageDigest md1, Provider p) {
- byte[] b = {1, 2, 3, 4, 5};
-
- if (!(md1 instanceof MyMessageDigest1)) {
- fail("getInstance() failed");
- }
- if (md1.getProvider() != p) {
- fail("getProvider() failed");
- }
- if (!"ABC".equals(md1.getAlgorithm())) {
- fail("getAlgorithm() failed");
- }
- md1.update((byte)1);
- md1.update(b, 1, 4);
- if (!((MyMessageDigest1)md1).runEngineUpdate1 ||
- !((MyMessageDigest1)md1).runEngineUpdate2) {
- fail("update failed");
- }
- try {
- if (md1.digest() != null) {
- fail("incorrect digest result");
- }
- if (md1.digest(b, 2, 3) != 0) {
- fail("incorrect digest result");
- }
- } catch (java.security.DigestException e) {
- fail(e.toString());
- }
- if (!((MyMessageDigest1)md1).runEngineDigest) {
- fail("update failed");
- }
- md1.reset();
- if (!((MyMessageDigest1)md1).runEngineReset) {
- fail("reset failed");
- }
- if (md1.getDigestLength() != 0) {
- fail("getDigestLength failed");
- }
- try {
- md1.clone();
- fail("No expected CloneNotSupportedException");
- } catch (CloneNotSupportedException e) {
- }
- }
-
- private void checkMD2(MessageDigest md2, Provider p) {
- byte[] b = {1, 2, 3, 4, 5};
-
- if (md2.getProvider() != p) {
- fail("getProvider() failed");
- }
- if (!"CBA".equals(md2.getAlgorithm())) {
- fail("getAlgorithm() failed");
- }
- md2.update((byte)1);
- md2.update(b, 1, 3);
- if (!MyMessageDigest2.runEngineUpdate1 ||
- !MyMessageDigest2.runEngineUpdate2) {
- fail("update failed");
- }
- try {
- if (md2.digest() != null) {
- fail("incorrect digest result");
- }
- if (md2.digest(b, 2, 3) != 0) {
- fail("incorrect digest result");
- }
- } catch (java.security.DigestException e) {
- fail(e.toString());
- }
- if (!MyMessageDigest2.runEngineDigest) {
- fail("update failed");
- }
- md2.reset();
- if (!MyMessageDigest2.runEngineReset) {
- fail("reset failed");
- }
- if (md2.getDigestLength() != 0) {
- fail("getDigestLength failed");
- }
- try {
- md2.clone();
- fail("No expected CloneNotSupportedException");
- } catch (CloneNotSupportedException e) {
- }
- }
+ public void testGetInstanceStringProvider2() throws Exception {
+ Provider p = new MyProvider();
+ MessageDigest md2 = MessageDigest.getInstance("CBA", p);
+ checkMD2(md2, p);
+ }
+
+ private void checkMD1(MessageDigest md1, Provider p) throws Exception {
+ byte[] b = { 1, 2, 3, 4, 5 };
+ assertTrue("getInstance() failed", md1 instanceof MyMessageDigest1);
+ assertEquals("getProvider() failed", p, md1.getProvider());
+ assertEquals("getAlgorithm() failed", "ABC", md1.getAlgorithm());
+
+ md1.update((byte) 1);
+ md1.update(b, 1, 4);
+ assertTrue("update failed", ((MyMessageDigest1) md1).runEngineUpdate1);
+ assertTrue("update failed", ((MyMessageDigest1) md1).runEngineUpdate2);
+
+ assertNull("incorrect digest result", md1.digest());
+ assertEquals("getProvider() failed", 0, md1.digest(b, 2, 3));
+ assertTrue("digest failed", ((MyMessageDigest1) md1).runEngineDigest);
+
+ md1.reset();
+ assertTrue("reset failed", ((MyMessageDigest1) md1).runEngineReset);
+
+ assertEquals("getDigestLength() failed", 0, md1.getDigestLength());
+ try {
+ md1.clone();
+ fail("No expected CloneNotSupportedException");
+ } catch (CloneNotSupportedException e) {
+ }
+ }
+
+ private void checkMD2(MessageDigest md2, Provider p) throws Exception {
+ byte[] b = { 1, 2, 3, 4, 5 };
+ assertEquals("getProvider() failed", p, md2.getProvider());
+ assertEquals("getAlgorithm() failed", "CBA", md2.getAlgorithm());
+
+ md2.update((byte) 1);
+ md2.update(b, 1, 3);
+ assertTrue("update failed", MyMessageDigest2.runEngineUpdate1);
+ assertTrue("update failed", MyMessageDigest2.runEngineUpdate2);
+
+ assertNull("incorrect digest result", md2.digest());
+ assertEquals("getProvider() failed", 0, md2.digest(b, 2, 3));
+ assertTrue("digest failed", MyMessageDigest2.runEngineDigest);
+
+ md2.reset();
+ assertTrue("reset failed", MyMessageDigest2.runEngineReset);
+
+ assertEquals("getDigestLength() failed", 0, md2.getDigestLength());
+ try {
+ md2.clone();
+ fail("No expected CloneNotSupportedException");
+ } catch (CloneNotSupportedException e) {
+ }
+ }
private class MyProvider extends Provider {
MyProvider() {
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/PermissionCollection_ImplTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/PermissionCollection_ImplTest.java?rev=413841&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/PermissionCollection_ImplTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/PermissionCollection_ImplTest.java Tue Jun 13 01:58:11 2006
@@ -0,0 +1,74 @@
+/*
+ * 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 org.apache.harmony.security.tests.java.security;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for <code>PermissionCollection</code>
+ *
+ */
+
+public class PermissionCollection_ImplTest extends TestCase {
+ // Bare extension to instantiate abstract PermissionCollection class
+ private static final class RealPermissionCollection extends PermissionCollection
+ {
+ final private Collection col;
+ public RealPermissionCollection(Collection col)
+ {
+ this.col = col;
+ }
+
+ public void add(Permission permission) {}
+
+ public Enumeration elements()
+ {
+ return col == null ? null : Collections.enumeration(col);
+ }
+
+ public boolean implies(Permission permission)
+ {
+ return false;
+ }
+ }
+
+ /** Test toString() transformation with different elements. */
+ public void testToString()
+ {
+ // no elements
+ PermissionCollection pc = new RealPermissionCollection(null);
+ String superString = pc.getClass().getName() + "@" + Integer.toHexString(pc.hashCode());
+ assertEquals("no elements", superString + " (\n)", pc.toString());
+
+ // several elements
+ pc = new RealPermissionCollection(Arrays.asList(new Object[]{"aaa", "bbb", "ccc"}));
+ superString = pc.getClass().getName() + "@" + Integer.toHexString(pc.hashCode());
+ assertEquals("several elements", superString + " (\n aaa\n bbb\n ccc\n)", pc.toString());
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Permissions_ImplTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Permissions_ImplTest.java?rev=413841&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Permissions_ImplTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Permissions_ImplTest.java Tue Jun 13 01:58:11 2006
@@ -0,0 +1,98 @@
+/*
+ * 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 org.apache.harmony.security.tests.java.security;
+import java.security.AllPermission;
+import java.security.BasicPermission;
+import java.security.Permission;
+import java.security.PermissionCollection;
+import java.security.Permissions;
+import java.security.SecurityPermission;
+import java.security.UnresolvedPermission;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for <code>Permissions</code>
+ *
+ */
+
+public class Permissions_ImplTest extends TestCase {
+
+ /**
+ * A permission is implied by this collection, if either of the following is
+ * true:
+ * <ul>
+ * <li>This collection contains AllPermission;
+ * <li>This collection has elements of the same type as the permission
+ * being checked, and they imply it;
+ * <li>This collection has UnresolvedPermissions which can be resolved to
+ * the checked type, and after resolving they imply the checked one.
+ * </ul>
+ * The only exception is UnresolvedPermission itself, which is effectively
+ * implied only by AllPermission
+ */
+ public void testImplies() {
+ Permissions ps = new Permissions();
+ Permission ap = new AllPermission();
+ Permission bp1 = new BasicPermission("jhb23jhg5") {
+ };
+ Permission bp2 = new BasicPermission("&%#&^$HJVH") {
+
+ public PermissionCollection newPermissionCollection() {
+ return null;
+ }
+ };
+ Permission sp1 = new SecurityPermission("a.b.c");
+ Permission sp2 = new SecurityPermission("a.b.*");
+ Permission sp3 = new SecurityPermission("a.*");
+ Permission up = new UnresolvedPermission(
+ "java.security.SecurityPermission", "*", null, null);
+
+ Permission[] arr = new Permission[] {
+ ap, bp1, bp2, sp1, sp2, up };
+ for (int i = 0; i < arr.length; i++) {
+ assertFalse(ps.implies(arr[i]));
+ }
+
+ ps.add(bp1);
+ assertTrue(ps.implies(bp1));
+ assertFalse(ps.implies(bp2));
+ assertFalse(ps.implies(ap));
+ assertFalse(ps.implies(sp1));
+
+ ps.add(sp2);
+ assertTrue(ps.implies(sp1));
+ assertTrue(ps.implies(sp2));
+ assertFalse(ps.implies(sp3));
+
+ ps.add(up);
+ assertFalse(ps.implies(up));
+ assertTrue(ps.implies(sp1));
+ assertTrue(ps.implies(sp2));
+ assertTrue(ps.implies(sp3));
+
+ ps.add(ap);
+ for (int i = 0; i < arr.length; i++) {
+ assertTrue(ps.implies(arr[i]));
+ }
+ }
+ }
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Policy_ImplTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Policy_ImplTest.java?rev=413841&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Policy_ImplTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Policy_ImplTest.java Tue Jun 13 01:58:11 2006
@@ -0,0 +1,100 @@
+/*
+ * 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 org.apache.harmony.security.tests.java.security;
+import java.security.AllPermission;
+import java.security.CodeSource;
+import java.security.PermissionCollection;
+import java.security.Policy;
+import java.security.ProtectionDomain;
+import java.security.Security;
+import java.security.SecurityPermission;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for <code>Policy</code>
+ *
+ */
+
+public class Policy_ImplTest extends TestCase {
+
+ public static class TestProvider extends Policy {
+
+ PermissionCollection pc;
+
+ public PermissionCollection getPermissions(CodeSource cs) {
+ return pc;
+ }
+
+ public void refresh() {
+ }
+ }
+
+ /**
+ * Tests loading of a default provider, both valid and invalid class
+ * references.
+ */
+ public void testGetPolicy_LoadDefaultProvider() {
+ Policy oldPolicy = Policy.getPolicy();
+ String POLICY_PROVIDER = "policy.provider";
+ String oldProvider = Security.getProperty(POLICY_PROVIDER);
+ try {
+ Security.setProperty(POLICY_PROVIDER, TestProvider.class.getName());
+ Policy.setPolicy(null);
+ Policy p = Policy.getPolicy();
+ assertNotNull(p);
+ assertEquals(TestProvider.class.getName(), p.getClass().getName());
+
+ Security.setProperty(POLICY_PROVIDER, "a.b.c.D");
+ Policy.setPolicy(null);
+ p = Policy.getPolicy();
+ assertNotNull(p);
+ //exact type of default provider does not matter
+ //assertEquals(DefaultPolicy.class.getName(), p.getClass().getName());
+ } finally {
+ Security.setProperty(POLICY_PROVIDER, (oldProvider == null) ? ""
+ : oldProvider);
+ Policy.setPolicy(oldPolicy);
+ }
+ }
+
+ /**
+ * Tests that implies() does proper permission evaluation.
+ */
+ public void testImplies() {
+ TestProvider policy = new TestProvider();
+ SecurityPermission sp = new SecurityPermission("abc");
+ policy.pc = sp.newPermissionCollection();
+
+ policy.pc.add(sp);
+ assertTrue(policy.implies(new ProtectionDomain(null, null), sp));
+ assertFalse(policy.implies(null, sp));
+ assertFalse(policy.implies(new ProtectionDomain(null, null), null));
+ assertFalse(policy.implies(null, null));
+
+ ProtectionDomain pd = new ProtectionDomain(null, policy.pc);
+ policy.pc = null;
+ assertTrue(policy.implies(pd, sp));
+ assertFalse(policy.implies(pd, new AllPermission()));
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/ProviderService_ImplTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/ProviderService_ImplTest.java?rev=413841&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/ProviderService_ImplTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/ProviderService_ImplTest.java Tue Jun 13 01:58:11 2006
@@ -0,0 +1,65 @@
+/*
+ * 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 org.apache.harmony.security.tests.java.security;
+
+import java.security.Provider;
+import java.util.HashMap;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests for <code>Provider.Service</code> constructor and methods
+ *
+ */
+public class ProviderService_ImplTest extends TestCase {
+
+ /*
+ * Class under test for String toString()
+ */
+ public void testToString() {
+ Provider p = new MyProvider();
+ Provider.Service s = new Provider.Service(p, "type", "algorithm",
+ "className", null, null);
+ assertEquals("first toString() failed",
+ "Provider MyProvider Service type.algorithm className",
+ s.toString());
+
+ HashMap hm = new HashMap();
+ hm.put("attribute", "value");
+ hm.put("KeySize", "1024");
+ hm.put("AAA", "BBB");
+
+ s = new Provider.Service(p, "type", "algorithm", "className",
+ null, hm);
+ assertTrue("second toString() failed", s.toString().startsWith(
+ "Provider MyProvider Service type.algorithm className\n"
+ + "Attributes "));
+ }
+
+ class MyProvider extends Provider {
+ MyProvider() {
+ super("MyProvider", 1.0, "Provider for testing");
+ put("MessageDigest.SHA-1", "SomeClassName");
+ }
+ }
+}
Copied: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/SecureRandom_ImplTest.java (from r412639, incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/SecureRandom2Test.java)
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/SecureRandom_ImplTest.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/SecureRandom_ImplTest.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/SecureRandom2Test.java&r1=412639&r2=413841&rev=413841&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/SecureRandom2Test.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/SecureRandom_ImplTest.java Tue Jun 13 01:58:11 2006
@@ -1,171 +1,158 @@
-/*
- * 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.Arrays;
-import java.util.Random;
-
-import junit.framework.TestCase;
-
-
-/**
- *
- * Tests for internal Secure Random implementation based on Random
- *
- */
-public class SecureRandom2Test extends TestCase {
-
- /**
- * Registered providers
- */
- Provider providers[] = Security.getProviders();
-
- /*
- * @see TestCase#setUp()
- */
- protected void setUp() throws Exception {
- super.setUp();
- // remove all registerd providers
- for (int i = 0; i < providers.length; i++) {
- Security.removeProvider(providers[i].getName());
- }
- }
-
- /*
- * @see TestCase#tearDown()
- */
- protected void tearDown() throws Exception {
- super.tearDown();
- // restore all registerd providers
- for (int i = 0; i < providers.length; i++) {
- Security.addProvider(providers[i]);
- }
- }
-
- /*
- * Class under test for void setSeed(long)
- */
- public final void testSetSeedlong() {
- SecureRandom sr = new SecureRandom();
- sr.setSeed(0);
- sr.setSeed(-1);
- sr.setSeed(11111111111L);
- }
-
- public final void testNextBytes() {
- SecureRandom sr = new SecureRandom();
- sr.nextBytes(new byte[20]);
- sr.nextBytes(new byte[1]);
-
- //Not specified behavior: throws NullPointerException if bytes is null
- try {
- sr.nextBytes(null);
- } catch (NullPointerException e) {
- }
- sr.nextBytes(new byte[5]);
- }
-
- /*
- * Class under test for void SecureRandom(byte[])
- */
- public final void testSecureRandombyteArray() {
- byte[] seed = {1,2,3,4,5,6,7,8};
- SecureRandom sr = new SecureRandom(seed);
-
- long l = 0;
- for (int i = 0; i < seed.length; i++) {
- l = (l << 8) | (seed[i] & 0xFF);
- }
- Random r = new Random(l);
- byte[] b1 = new byte[8];
- byte[] b2 = new byte[8];
- sr.nextBytes(b1);
- r.nextBytes(b2);
- if (!Arrays.equals(b1, b2)) {
- fail("incorrect random bytes");
- }
- }
-
-
- /*
- * Class under test for SecureRandom getInstance(String)
- */
- public final void testGetInstanceString() {
- try {
- SecureRandom.getInstance("SHA1PRNG");
- fail("No expected NoSuchAlgorithmException");
- } catch (NoSuchAlgorithmException e) {
- }
- }
-
-
- public final void testGetProvider() {
- if (new SecureRandom().getProvider() != null) {
- fail("Non null provider");
- }
- }
-
- public final void testGetAlgorithm() {
- SecureRandom sr = new SecureRandom();
- if (!sr.getAlgorithm().equals("java.util.Random")) {
- fail("Incorrect algorithm");
- }
- }
-
- /*
- * Class under test for void setSeed(byte[])
- */
- public final void testSetSeedbyteArray() {
- SecureRandom sr = new SecureRandom();
-
- //Not specified behavior: throws NullPointerException if bytes is null
- try {
- sr.setSeed(null);
- } catch (NullPointerException e) {
- }
-
- byte[] seed = {1,2,3,4};
- sr.setSeed(seed);
-
- byte[] seed1 = {1,2,3,4, -2, 100, 9, 111};
- sr.setSeed(seed1);
- }
-
- /**
- *
- *
- */
- public final void testGetSeed() {
- byte[] seed = SecureRandom.getSeed(5);
- new SecureRandom(seed).nextBytes(new byte[20]);
- }
-
- /**
- *
- *
- */
- public final void testGenerateSeed() {
- SecureRandom sr = new SecureRandom();
- byte[] seed = sr.generateSeed(5);
- new SecureRandom(seed).nextBytes(new byte[20]);
- }
-}
+/*
+ * 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 org.apache.harmony.security.tests.java.security;
+
+import java.security.NoSuchAlgorithmException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.util.Arrays;
+import java.util.Random;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ * Tests for internal Secure Random implementation based on Random
+ *
+ */
+public class SecureRandom_ImplTest extends TestCase {
+
+ /**
+ * Registered providers
+ */
+ Provider providers[] = Security.getProviders();
+
+ /*
+ * @see TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ // remove all registerd providers
+ for (int i = 0; i < providers.length; i++) {
+ Security.removeProvider(providers[i].getName());
+ }
+ }
+
+ /*
+ * @see TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ // restore all registerd providers
+ for (int i = 0; i < providers.length; i++) {
+ Security.addProvider(providers[i]);
+ }
+ }
+
+ /*
+ * Class under test for void setSeed(long)
+ */
+ public final void testSetSeedlong() {
+ SecureRandom sr = new SecureRandom();
+ sr.setSeed(0);
+ sr.setSeed(-1);
+ sr.setSeed(11111111111L);
+ }
+
+ public final void testNextBytes() {
+ SecureRandom sr = new SecureRandom();
+ sr.nextBytes(new byte[20]);
+ sr.nextBytes(new byte[1]);
+
+ //Not specified behavior: throws NullPointerException if bytes is null
+ try {
+ sr.nextBytes(null);
+ } catch (NullPointerException e) {
+ }
+ sr.nextBytes(new byte[5]);
+ }
+
+ /*
+ * Class under test for void SecureRandom(byte[])
+ */
+ public final void testSecureRandombyteArray() {
+ byte[] seed = {1,2,3,4,5,6,7,8};
+ SecureRandom sr = new SecureRandom(seed);
+
+ long l = 0;
+ for (int i = 0; i < seed.length; i++) {
+ l = (l << 8) | (seed[i] & 0xFF);
+ }
+ Random r = new Random(l);
+ byte[] b1 = new byte[8];
+ byte[] b2 = new byte[8];
+ sr.nextBytes(b1);
+ r.nextBytes(b2);
+ assertTrue("incorrect random bytes", Arrays.equals(b1, b2));
+ }
+
+ /*
+ * Class under test for SecureRandom getInstance(String)
+ */
+ public final void testGetInstanceString() {
+ try {
+ SecureRandom.getInstance("SHA1PRNG");
+ fail("No expected NoSuchAlgorithmException");
+ } catch (NoSuchAlgorithmException e) {
+ }
+ }
+
+ public final void testGetProvider() {
+ assertNull("Non null provider", new SecureRandom().getProvider());
+ }
+
+ public final void testGetAlgorithm() {
+ SecureRandom sr = new SecureRandom();
+ assertEquals("Incorrect algorithm", "java.util.Random", sr.getAlgorithm());
+ }
+
+ /*
+ * Class under test for void setSeed(byte[])
+ */
+ public final void testSetSeedbyteArray() {
+ SecureRandom sr = new SecureRandom();
+
+ //Not specified behavior: throws NullPointerException if bytes is null
+ try {
+ sr.setSeed(null);
+ } catch (NullPointerException e) {
+ }
+
+ byte[] seed = {1,2,3,4};
+ sr.setSeed(seed);
+
+ byte[] seed1 = {1,2,3,4, -2, 100, 9, 111};
+ sr.setSeed(seed1);
+ }
+
+ public final void testGetSeed() {
+ byte[] seed = SecureRandom.getSeed(5);
+ new SecureRandom(seed).nextBytes(new byte[20]);
+ }
+
+ public final void testGenerateSeed() {
+ SecureRandom sr = new SecureRandom();
+ byte[] seed = sr.generateSeed(5);
+ new SecureRandom(seed).nextBytes(new byte[20]);
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Timestamp_ImplTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Timestamp_ImplTest.java?rev=413841&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Timestamp_ImplTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/Timestamp_ImplTest.java Tue Jun 13 01:58:11 2006
@@ -0,0 +1,51 @@
+/*
+ * 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 Alexander V. Astapchuk
+* @version $Revision$
+*/
+
+package org.apache.harmony.security.tests.java.security;
+
+import java.security.Timestamp;
+import java.security.cert.CertPath;
+import java.util.Date;
+
+import org.apache.harmony.security.tests.support.cert.MyCertPath;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Tests for <code>Timestamp</code> class fields and methods
+ *
+ */
+
+public class Timestamp_ImplTest extends TestCase {
+
+ private Date now = new Date();
+
+ private static final byte[] encoding = { 1, 2, 3 };
+
+ private CertPath cpath = new MyCertPath(encoding);
+
+ public void testHashCode() {
+ assertTrue(new Timestamp(now, cpath).hashCode() == (now.hashCode() ^ cpath
+ .hashCode()));
+ }
+
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/KeyStoreTestSupport.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/KeyStoreTestSupport.java?rev=413841&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/KeyStoreTestSupport.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/KeyStoreTestSupport.java Tue Jun 13 01:58:11 2006
@@ -0,0 +1,176 @@
+/*
+ * 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.
+ */
+
+package org.apache.harmony.security.tests.support;
+
+import java.security.InvalidKeyException;
+import java.security.KeyStore;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.security.SignatureException;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateEncodingException;
+import java.security.cert.CertificateException;
+
+import javax.crypto.SecretKey;
+
+/**
+ * Support class for KeyStore tests
+ *
+ */
+
+public class KeyStoreTestSupport {
+
+ public static final String srvKeyStore = "KeyStore";
+
+ public static String[] validValues = { "bks", "BKS", "bKS", "Bks", "bKs",
+ "BkS" };
+
+ public static String defaultType = "bks";
+
+ public static boolean JKSSupported = false;
+
+ public static String defaultProviderName = null;
+
+ public static Provider defaultProvider = null;
+
+ static {
+ defaultProvider = SpiEngUtils.isSupport(defaultType, srvKeyStore);
+ JKSSupported = (defaultProvider != null);
+ defaultProviderName = (JKSSupported ? defaultProvider.getName() : null);
+ }
+
+ /**
+ * Additional class to create SecretKey object
+ */
+ public static class SKey implements SecretKey {
+ private String type;
+
+ private byte[] encoded;
+
+ public SKey(String type, byte[] encoded) {
+ this.type = type;
+ this.encoded = encoded;
+ }
+
+ public String getAlgorithm() {
+ return type;
+ }
+
+ public byte[] getEncoded() {
+ return encoded;
+ }
+
+ public String getFormat() {
+ return "test";
+ }
+ }
+
+ /**
+ * Additional class to create PrivateKey object
+ */
+ public static class MyPrivateKey implements PrivateKey {
+ private String algorithm;
+
+ private String format;
+
+ private byte[] encoded;
+
+ public MyPrivateKey(String algorithm, String format, byte[] encoded) {
+ this.algorithm = algorithm;
+ this.format = format;
+ this.encoded = encoded;
+ }
+
+ public String getAlgorithm() {
+ return algorithm;
+ }
+
+ public String getFormat() {
+ return format;
+ }
+
+ public byte[] getEncoded() {
+ return encoded;
+ }
+ }
+
+ /**
+ * Additional class to create Certificate and Key objects
+ */
+ public static class MCertificate extends Certificate {
+ private final byte[] encoding;
+
+ private final String type;
+
+ public MCertificate(String type, byte[] encoding) {
+ super(type);
+ this.encoding = encoding;
+ this.type = type;
+ }
+
+ public byte[] getEncoded() throws CertificateEncodingException {
+ return encoding.clone();
+ }
+
+ public void verify(PublicKey key) throws CertificateException,
+ NoSuchAlgorithmException, InvalidKeyException,
+ NoSuchProviderException, SignatureException {
+ }
+
+ public void verify(PublicKey key, String sigProvider)
+ throws CertificateException, NoSuchAlgorithmException,
+ InvalidKeyException, NoSuchProviderException,
+ SignatureException {
+ }
+
+ public String toString() {
+ return "[MCertificate, type: " + getType() + "]";
+ }
+
+ public PublicKey getPublicKey() {
+ return new PublicKey() {
+ public String getAlgorithm() {
+ return type;
+ }
+
+ public byte[] getEncoded() {
+ return encoding;
+ }
+
+ public String getFormat() {
+ return "test";
+ }
+ };
+ }
+ }
+
+ /**
+ * Additional class to create ProtectionParameter object
+ */
+ public static class ProtPar implements KeyStore.ProtectionParameter {
+ }
+
+ /**
+ * Additional class to create KeyStore.Entry object
+ */
+ public static class AnotherEntry implements KeyStore.Entry {
+ }
+}
+
Copied: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/MyKeyStore.java (from r412639, incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/MyKeyStore.java)
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/MyKeyStore.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/MyKeyStore.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/MyKeyStore.java&r1=412639&r2=413841&rev=413841&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/MyKeyStore.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/MyKeyStore.java Tue Jun 13 01:58:11 2006
@@ -19,7 +19,7 @@
* @version $Revision$
*/
-package java.security;
+package org.apache.harmony.security.tests.support;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -37,8 +37,6 @@
import java.util.Enumeration;
import java.util.Hashtable;
-import org.apache.harmony.security.tests.support.MyLoadStoreParams;
-
/**
* Additional class for KeyStoreSpi and KeyStore verification
*
@@ -113,7 +111,7 @@
if (Chain.containsKey(alias)) {
Chain.remove(alias);
}
- KeyStore1Test.MyPrivateKey keyK = new KeyStore1Test("proba").new MyPrivateKey(
+ KeyStoreTestSupport.MyPrivateKey keyK = new KeyStoreTestSupport.MyPrivateKey(
alias, alias, key);
Keys.put(alias, keyK);
if (chain != null) {
Copied: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/RandomImpl.java (from r412639, incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/RandomImpl.java)
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/RandomImpl.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/RandomImpl.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/RandomImpl.java&r1=412639&r2=413841&rev=413841&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/api/java.injected/java/security/RandomImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/RandomImpl.java Tue Jun 13 01:58:11 2006
@@ -18,7 +18,9 @@
* @version $Revision$
*/
-package java.security;
+package org.apache.harmony.security.tests.support;
+
+import java.security.SecureRandomSpi;
/**
* Test implementation of SecureRandom
@@ -32,7 +34,6 @@
protected void engineSetSeed(byte[] seed) {
runEngineSetSeed = true;
-
}
protected void engineNextBytes(byte[] bytes) {
Modified: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SerializationTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SerializationTest.java?rev=413841&r1=413840&r2=413841&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SerializationTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/support/common/java/org/apache/harmony/security/tests/support/SerializationTest.java Tue Jun 13 01:58:11 2006
@@ -1,308 +1,308 @@
-/*
- * 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 org.apache.harmony.security.tests.support;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.OutputStream;
-import java.io.Serializable;
-import java.lang.reflect.Method;
-
-import junit.framework.TestCase;
-
-/**
- * Framework for serialization testing. Subclasses only need to override
- * getData() method and, optionally, assertDeserialized() method. The first one
- * returns array of objects to be de/serialized in tests, and the second
- * compares reference and deserialized objects (needed only if tested objects do
- * not provide specific method equals()). <br>
- * There are two modes of test run: <b>reference generation mode </b> and
- * <b>testing mode </b>. The actual mode is selected via
- * <b>"test.mode" </b> system property. The <b>testing mode </b> is
- * the default mode. <br>
- * To turn on the <b>reference generation mode </b>, the test.mode property
- * should be set to value "serial.reference". In this mode, no testing
- * is performed but golden files are produced, which contain reference
- * serialized objects. This mode should be run on a pure
- * Implementation classes, which are targeted for compartibility. <br>
- * The location of golden files (in both modes) is controlled via
- * <b>"RESOURCE_DIR" </b> system property.
- *
- */
-public abstract class SerializationTest extends TestCase {
-
- /**
- * Property name for the testing mode.
- */
- public static final String MODE_KEY = "test.mode";
-
-
- /**
- * Testing mode.
- */
- public static String mode = System.getProperty(MODE_KEY);
-
- /**
- * Reference files generation mode switch.
- */
- public static final String SERIAL_REFERENCE_MODE = "serial.reference";
-
- /**
- * Key to a system property defining root location of golden files.
- */
- public static final String GOLDEN_PATH = "RESOURCE_DIR";
-
- private static String outputPath = System.getProperty(GOLDEN_PATH,
- "test/common/unit");
-
- /**
- * Parameterized c-tor inherited from superclass.
- */
- public SerializationTest(String name) {
- super(name);
- }
-
- /**
- * Default c-tor inherited from superclass.
- */
- public SerializationTest() {
- super();
- }
-
- /**
- * Depending on testing mode, produces golden files or performs testing.
- */
- public void runBare() throws Throwable {
-
- if (mode != null && mode.equals(SERIAL_REFERENCE_MODE)) {
- produceGoldenFiles();
- } else {
- super.runBare();
- }
- }
-
- /**
- * Returns zero value to exclude serialization tests from performance runs.
- */
- public long getRepeatCount() {
- return 0;
- }
-
- /**
- * This is the main working method of this framework. Subclasses must
- * override it to provide actual objects for testing.
- *
- * @return array of objects to be de/serialized in tests.
- */
- protected abstract Object[] getData();
-
- /**
- * Compares deserialized and reference objects. This default implementation
- * just asserts equality of the two objects. Should be overriden if a class
- * under test does not provide specific equals() method and it's instances
- * should to be compared manually.
- */
- protected void assertDeserialized(Object reference, Object test) {
- assertEquals(reference, test);
- }
-
- /**
- * Tests that data objects can be serialized and deserialized without
- * exceptions, and that deserialization really produces deeply cloned
- * objects.
- */
- public void testSelf() throws Throwable {
-
- SerializableAssert comparator = defineComparator();
-
- Object[] data = getData();
- for (int i = 0; i < data.length; i++) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- putObjectToStream(data[i], bos);
- ByteArrayInputStream bis = new ByteArrayInputStream(bos
- .toByteArray());
-
- comparator.assertDeserialized((Serializable) data[i],
- (Serializable) getObjectFromStream(bis));
- }
- }
-
- /**
- * Tests that data objects can be deserialized from golden files, to verify
- * compartibility with Reference Implementation.
- */
- public void testGolden() throws Throwable {
-
- SerializableAssert comparator = defineComparator();
-
- Object[] data = getData();
- for (int i = 0; i < data.length; i++) {
- comparator.assertDeserialized((Serializable) data[i],
- (Serializable) getObjectFromStream(new FileInputStream(
- getDataFile(i))));
- }
- }
-
- /**
- * Returns golden file for an object being tested.
- *
- * @param index array index of tested data (as returned by
- * {@link #getData() getData()})
- * @return corresponding golden file
- */
- protected File getDataFile(int index) {
- String name = this.getClass().getName();
- int dot = name.lastIndexOf(".");
- String path = name.substring(0, dot).replace('.', File.separatorChar);
- if (outputPath != null && outputPath.length() != 0) {
- path = outputPath + File.separator + path;
- }
-
- return new File(path, name.substring(dot + 1) + "." + index + ".dat");
- }
-
- /**
- * Working method for files generation mode. Serializes test objects
- * returned by {@link #getData() getData()}to golden files, each object to
- * a separate file.
- *
- * @throws IOException
- */
- protected void produceGoldenFiles() throws IOException {
- Object[] data = getData();
- for (int i = 0; i < data.length; i++) {
- File gf = getDataFile(i);
- gf.getParentFile().mkdirs();
- gf.createNewFile();
- putObjectToStream(data[i], new FileOutputStream(gf));
- }
- }
-
- /**
- * Serializes specified object to an output stream.
- */
- protected void putObjectToStream(Object obj, OutputStream os)
- throws IOException {
- ObjectOutputStream oos = new ObjectOutputStream(os);
- oos.writeObject(obj);
- oos.flush();
- oos.close();
- }
-
- /**
- * Deserializes single object from an input stream.
- */
- protected Object getObjectFromStream(InputStream is) throws IOException,
- ClassNotFoundException {
- ObjectInputStream ois = new ObjectInputStream(is);
- Object result = ois.readObject();
- ois.close();
- return result;
- }
-
- /**
- * Interface to compare (de)serialized objects
- */
- public interface SerializableAssert {
- void assertDeserialized(Serializable reference, Serializable test);
- }
-
- // default comparator for a class that has equals(Object) method
- private final static SerializableAssert DEFAULT_COMPARATOR = new SerializableAssert() {
- public void assertDeserialized(Serializable reference, Serializable test) {
- TestCase.assertEquals(reference, test);
- }
- };
-
- // for comparing java.lang.Throwable objects
- private final static SerializableAssert THROWABLE_COMPARATOR = new SerializableAssert() {
- public void assertDeserialized(Serializable reference, Serializable test) {
-
- Throwable refThr = (Throwable) reference;
- Throwable tstThr = (Throwable) test;
-
- // verify class
- TestCase.assertEquals(refThr.getClass(), tstThr.getClass());
-
- // verify message
- TestCase.assertEquals(refThr.getMessage(), tstThr.getMessage());
-
- // verify cause
- if (refThr.getCause() == null) {
- TestCase.assertNull(tstThr.getCause());
- } else {
- TestCase.assertNotNull(tstThr.getCause());
-
- refThr = refThr.getCause();
- tstThr = tstThr.getCause();
-
- TestCase.assertEquals(refThr.getClass(), tstThr.getClass());
- TestCase.assertEquals(refThr.getMessage(), tstThr.getMessage());
- }
- }
- };
-
- private SerializableAssert defineComparator() throws Exception {
-
- if (this instanceof SerializableAssert) {
- return (SerializableAssert) this;
- }
-
- Object s[] = getData();
- if (s == null || s.length == 0) {
- // nothing to compare - OK with default comparator
- return DEFAULT_COMPARATOR;
-
- }
-
- Method m = s[0].getClass().getMethod("equals", new Class[] {Object.class});
-
- if (m.getDeclaringClass() != Object.class) {
- // one of classes overrides Object.equals(Object) method
- // use default comparator
- return DEFAULT_COMPARATOR;
- }
-
- // TODO use generics to detect comparator
- // instead of 'instanceof' for the first element
- if(s[0] instanceof java.lang.Throwable){
- return THROWABLE_COMPARATOR;
- }
-
- // TODO - throw new RuntimeException() if failed to detect comparator
- // return stub comparator for a while
- final SerializationTest thisTest = this;
- return new SerializableAssert() {
- public void assertDeserialized(Serializable reference,
- Serializable test) {
- thisTest.assertDeserialized(reference, test);
- }
- };
- }
-}
+/*
+ * 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 org.apache.harmony.security.tests.support;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.lang.reflect.Method;
+
+import junit.framework.TestCase;
+
+/**
+ * Framework for serialization testing. Subclasses only need to override
+ * getData() method and, optionally, assertDeserialized() method. The first one
+ * returns array of objects to be de/serialized in tests, and the second
+ * compares reference and deserialized objects (needed only if tested objects do
+ * not provide specific method equals()). <br>
+ * There are two modes of test run: <b>reference generation mode </b> and
+ * <b>testing mode </b>. The actual mode is selected via
+ * <b>"test.mode" </b> system property. The <b>testing mode </b> is
+ * the default mode. <br>
+ * To turn on the <b>reference generation mode </b>, the test.mode property
+ * should be set to value "serial.reference". In this mode, no testing
+ * is performed but golden files are produced, which contain reference
+ * serialized objects. This mode should be run on a pure
+ * Implementation classes, which are targeted for compartibility. <br>
+ * The location of golden files (in both modes) is controlled via
+ * <b>"RESOURCE_DIR" </b> system property.
+ *
+ */
+public abstract class SerializationTest extends TestCase {
+
+ /**
+ * Property name for the testing mode.
+ */
+ public static final String MODE_KEY = "test.mode";
+
+
+ /**
+ * Testing mode.
+ */
+ public static String mode = System.getProperty(MODE_KEY);
+
+ /**
+ * Reference files generation mode switch.
+ */
+ public static final String SERIAL_REFERENCE_MODE = "serial.reference";
+
+ /**
+ * Key to a system property defining root location of golden files.
+ */
+ public static final String GOLDEN_PATH = "RESOURCE_DIR";
+
+ private static String outputPath = System.getProperty(GOLDEN_PATH,
+ "test/common/unit");
+
+ /**
+ * Parameterized c-tor inherited from superclass.
+ */
+ public SerializationTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Default c-tor inherited from superclass.
+ */
+ public SerializationTest() {
+ super();
+ }
+
+ /**
+ * Depending on testing mode, produces golden files or performs testing.
+ */
+ public void runBare() throws Throwable {
+
+ if (mode != null && mode.equals(SERIAL_REFERENCE_MODE)) {
+ produceGoldenFiles();
+ } else {
+ super.runBare();
+ }
+ }
+
+ /**
+ * Returns zero value to exclude serialization tests from performance runs.
+ */
+ public long getRepeatCount() {
+ return 0;
+ }
+
+ /**
+ * This is the main working method of this framework. Subclasses must
+ * override it to provide actual objects for testing.
+ *
+ * @return array of objects to be de/serialized in tests.
+ */
+ protected abstract Object[] getData();
+
+ /**
+ * Compares deserialized and reference objects. This default implementation
+ * just asserts equality of the two objects. Should be overriden if a class
+ * under test does not provide specific equals() method and it's instances
+ * should to be compared manually.
+ */
+ protected void assertDeserialized(Object reference, Object test) {
+ assertEquals(reference, test);
+ }
+
+ /**
+ * Tests that data objects can be serialized and deserialized without
+ * exceptions, and that deserialization really produces deeply cloned
+ * objects.
+ */
+ public void testSelf() throws Throwable {
+
+ SerializableAssert comparator = defineComparator();
+
+ Object[] data = getData();
+ for (int i = 0; i < data.length; i++) {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ putObjectToStream(data[i], bos);
+ ByteArrayInputStream bis = new ByteArrayInputStream(bos
+ .toByteArray());
+
+ comparator.assertDeserialized((Serializable) data[i],
+ (Serializable) getObjectFromStream(bis));
+ }
+ }
+
+ /**
+ * Tests that data objects can be deserialized from golden files, to verify
+ * compartibility with Reference Implementation.
+ */
+ public void testGolden() throws Throwable {
+
+ SerializableAssert comparator = defineComparator();
+
+ Object[] data = getData();
+ for (int i = 0; i < data.length; i++) {
+ comparator.assertDeserialized((Serializable) data[i],
+ (Serializable) getObjectFromStream(new FileInputStream(
+ getDataFile(i))));
+ }
+ }
+
+ /**
+ * Returns golden file for an object being tested.
+ *
+ * @param index array index of tested data (as returned by
+ * {@link #getData() getData()})
+ * @return corresponding golden file
+ */
+ protected File getDataFile(int index) {
+ String name = this.getClass().getName();
+ int dot = name.lastIndexOf(".");
+ String path = name.substring(0, dot).replace('.', File.separatorChar);
+ if (outputPath != null && outputPath.length() != 0) {
+ path = outputPath + File.separator + path;
+ }
+
+ return new File(path, name.substring(dot + 1) + "." + index + ".dat");
+ }
+
+ /**
+ * Working method for files generation mode. Serializes test objects
+ * returned by {@link #getData() getData()}to golden files, each object to
+ * a separate file.
+ *
+ * @throws IOException
+ */
+ protected void produceGoldenFiles() throws IOException {
+ Object[] data = getData();
+ for (int i = 0; i < data.length; i++) {
+ File gf = getDataFile(i);
+ gf.getParentFile().mkdirs();
+ gf.createNewFile();
+ putObjectToStream(data[i], new FileOutputStream(gf));
+ }
+ }
+
+ /**
+ * Serializes specified object to an output stream.
+ */
+ protected void putObjectToStream(Object obj, OutputStream os)
+ throws IOException {
+ ObjectOutputStream oos = new ObjectOutputStream(os);
+ oos.writeObject(obj);
+ oos.flush();
+ oos.close();
+ }
+
+ /**
+ * Deserializes single object from an input stream.
+ */
+ protected Object getObjectFromStream(InputStream is) throws IOException,
+ ClassNotFoundException {
+ ObjectInputStream ois = new ObjectInputStream(is);
+ Object result = ois.readObject();
+ ois.close();
+ return result;
+ }
+
+ /**
+ * Interface to compare (de)serialized objects
+ */
+ public interface SerializableAssert {
+ void assertDeserialized(Serializable reference, Serializable test);
+ }
+
+ // default comparator for a class that has equals(Object) method
+ private final static SerializableAssert DEFAULT_COMPARATOR = new SerializableAssert() {
+ public void assertDeserialized(Serializable reference, Serializable test) {
+ TestCase.assertEquals(reference, test);
+ }
+ };
+
+ // for comparing java.lang.Throwable objects
+ private final static SerializableAssert THROWABLE_COMPARATOR = new SerializableAssert() {
+ public void assertDeserialized(Serializable reference, Serializable test) {
+
+ Throwable refThr = (Throwable) reference;
+ Throwable tstThr = (Throwable) test;
+
+ // verify class
+ TestCase.assertEquals(refThr.getClass(), tstThr.getClass());
+
+ // verify message
+ TestCase.assertEquals(refThr.getMessage(), tstThr.getMessage());
+
+ // verify cause
+ if (refThr.getCause() == null) {
+ TestCase.assertNull(tstThr.getCause());
+ } else {
+ TestCase.assertNotNull(tstThr.getCause());
+
+ refThr = refThr.getCause();
+ tstThr = tstThr.getCause();
+
+ TestCase.assertEquals(refThr.getClass(), tstThr.getClass());
+ TestCase.assertEquals(refThr.getMessage(), tstThr.getMessage());
+ }
+ }
+ };
+
+ private SerializableAssert defineComparator() throws Exception {
+
+ if (this instanceof SerializableAssert) {
+ return (SerializableAssert) this;
+ }
+
+ Object s[] = getData();
+ if (s == null || s.length == 0) {
+ // nothing to compare - OK with default comparator
+ return DEFAULT_COMPARATOR;
+
+ }
+
+ Method m = s[0].getClass().getMethod("equals", new Class[] {Object.class});
+
+ if (m.getDeclaringClass() != Object.class) {
+ // one of classes overrides Object.equals(Object) method
+ // use default comparator
+ return DEFAULT_COMPARATOR;
+ }
+
+ // TODO use generics to detect comparator
+ // instead of 'instanceof' for the first element
+ if(s[0] instanceof java.lang.Throwable){
+ return THROWABLE_COMPARATOR;
+ }
+
+ // TODO - throw new RuntimeException() if failed to detect comparator
+ // return stub comparator for a while
+ final SerializationTest thisTest = this;
+ return new SerializableAssert() {
+ public void assertDeserialized(Serializable reference,
+ Serializable test) {
+ thisTest.assertDeserialized(reference, test);
+ }
+ };
+ }
+}