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/04/10 07:38:38 UTC
svn commit: r392891 [2/5] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/auth/make/common/ modules/auth/src/test/java/common/tests/
modules/auth/src/test/java/common/tests/api/
modules/auth/src/test/java/common/tests/api/javax/ modules/...
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/BasicPermissionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/BasicPermissionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/BasicPermissionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/BasicPermissionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,135 @@
+/* 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 tests.api.java.security;
+
+import java.security.BasicPermission;
+import java.security.PermissionCollection;
+
+public class BasicPermissionTest extends junit.framework.TestCase {
+
+ public static class BasicPermissionSubclass extends BasicPermission {
+ public BasicPermissionSubclass(String name) {
+ super(name);
+ }
+
+ public BasicPermissionSubclass(String name, String actions) {
+ super(name, actions);
+ }
+ }
+
+ BasicPermission bp = new BasicPermissionSubclass("aName");
+
+ BasicPermission bp2 = new BasicPermissionSubclass("aName", "anAction");
+
+ BasicPermission bp3 = new BasicPermissionSubclass("*");
+
+ BasicPermission bp4 = new BasicPermissionSubclass("this.that");
+
+ BasicPermission bp5 = new BasicPermissionSubclass("this.*");
+
+ /**
+ * @tests java.security.BasicPermission#BasicPermission(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.security.BasicPermission(java.lang.String)
+ assertEquals("Incorrect name returned", "aName", bp.getName());
+ }
+
+ /**
+ * @tests java.security.BasicPermission#BasicPermission(java.lang.String,
+ * java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_StringLjava_lang_String() {
+ // Test for method java.security.BasicPermission(java.lang.String,
+ // java.lang.String)
+ assertEquals("Incorrect name returned", "aName", bp2.getName());
+ }
+
+ /**
+ * @tests java.security.BasicPermission#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ // Test for method boolean
+ // java.security.BasicPermission.equals(java.lang.Object)
+ assertTrue("a) Equal objects returned non-equal", bp.equals(bp2));
+ assertTrue("b) Equal objects returned non-equal", bp2.equals(bp));
+ assertTrue("a) Unequal objects returned equal", !bp.equals(bp3));
+ assertTrue("b) Unequal objects returned equal", !bp4.equals(bp5));
+ }
+
+ /**
+ * @tests java.security.BasicPermission#getActions()
+ */
+ public void test_getActions() {
+ // Test for method java.lang.String
+ // java.security.BasicPermission.getActions()
+ assertTrue("a) Incorrect actions returned, wanted the empty String", bp
+ .getActions().equals(""));
+ assertTrue("b) Incorrect actions returned, wanted the empty String",
+ bp2.getActions().equals(""));
+ }
+
+ /**
+ * @tests java.security.BasicPermission#hashCode()
+ */
+ public void test_hashCode() {
+ // Test for method int java.security.BasicPermission.hashCode()
+ assertTrue("Equal objects should return same hash",
+ bp.hashCode() == bp2.hashCode());
+ }
+
+ /**
+ * @tests java.security.BasicPermission#implies(java.security.Permission)
+ */
+ public void test_impliesLjava_security_Permission() {
+ // Test for method boolean
+ // java.security.BasicPermission.implies(java.security.Permission)
+ assertTrue("Equal objects should imply eachother", bp.implies(bp2));
+ assertTrue("a) should not imply", !bp.implies(bp3));
+ assertTrue("b) should not imply", !bp4.implies(bp5));
+ assertTrue("a) should imply", bp3.implies(bp5));
+ assertTrue("b) should imply", bp5.implies(bp4));
+
+ }
+
+ /**
+ * @tests java.security.BasicPermission#newPermissionCollection()
+ */
+ public void test_newPermissionCollection() {
+ // Test for method java.security.PermissionCollection
+ // java.security.BasicPermission.newPermissionCollection()
+ PermissionCollection bpc = bp.newPermissionCollection();
+ bpc.add(bp5);
+ bpc.add(bp);
+ assertTrue("Should imply", bpc.implies(bp4));
+ assertTrue("Should not imply", !bpc.implies(bp3));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/CodeSourceTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/CodeSourceTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/CodeSourceTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/CodeSourceTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,159 @@
+/* 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 tests.api.java.security;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.CodeSource;
+import java.security.cert.Certificate;
+
+public class CodeSourceTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.CodeSource#CodeSource(java.net.URL,
+ * java.security.cert.Certificate[])
+ */
+ public void test_ConstructorLjava_net_URL$Ljava_security_cert_Certificate() {
+ // Test for method java.security.CodeSource(java.net.URL,
+ // java.security.cert.Certificate [])
+ try {
+ new CodeSource(new java.net.URL("file:///test"),
+ (Certificate[]) null);
+ } catch (Exception e) {
+ fail("Caught an exception: " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.CodeSource#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ // Test for method boolean
+ // java.security.CodeSource.equals(java.lang.Object)
+ try {
+ CodeSource cs1 = new CodeSource(new java.net.URL("file:///test"),
+ (Certificate[]) null);
+ CodeSource cs2 = new CodeSource(new java.net.URL("file:///test"),
+ (Certificate[]) null);
+ assertTrue("Identical objects were not equal()!", cs1.equals(cs2));
+ } catch (Exception e) {
+ fail("Caught an exception: " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.CodeSource#hashCode()
+ */
+ public void test_hashCode() {
+ try {
+ URL url = new java.net.URL("file:///test");
+ CodeSource cs = new CodeSource(url, (Certificate[]) null);
+ assertTrue("Did not get expected hashCode!", cs.hashCode() == url
+ .hashCode());
+ } catch (Exception e) {
+ fail("Caught an exception: " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.CodeSource#getCertificates()
+ */
+ public void test_getCertificates() {
+ // Test for method java.security.cert.Certificate []
+ // java.security.CodeSource.getCertificates()
+ try {
+ CodeSource cs = new CodeSource(new java.net.URL("file:///test"),
+ (Certificate[]) null);
+ assertTrue("Should have gotten null certificate list.", cs
+ .getCertificates() == null);
+ } catch (Exception e) {
+ fail("Caught an exception: " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.CodeSource#getLocation()
+ */
+ public void test_getLocation() {
+ // Test for method java.net.URL java.security.CodeSource.getLocation()
+ try {
+ CodeSource cs = new CodeSource(new java.net.URL("file:///test"),
+ (Certificate[]) null);
+ assertTrue("Did not get expected location!", cs.getLocation()
+ .toString().equals("file:/test"));
+ } catch (Exception e) {
+ fail("Caught an exception: " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.CodeSource#implies(java.security.CodeSource)
+ */
+ public void test_impliesLjava_security_CodeSource() {
+ // Test for method boolean
+ // java.security.CodeSource.implies(java.security.CodeSource)
+ try {
+ CodeSource cs1 = new CodeSource(new URL("file:/d:/somedir"),
+ (Certificate[]) null);
+ CodeSource cs2 = new CodeSource(new URL("file:/d:/somedir/"),
+ (Certificate[]) (Certificate[]) null);
+ assertTrue("Does not add /", cs1.implies(cs2));
+ } catch (MalformedURLException e) {
+ fail("Caught MalformedURLException : " + e.toString());
+ }
+
+ try {
+ CodeSource cs1 = new CodeSource(new URL("file", null, -1,
+ "/d:/somedir/"), (Certificate[]) null);
+ CodeSource cs2 = new CodeSource(new URL("file:/d:/somedir/"),
+ (Certificate[]) null);
+ assertTrue("null host should imply host", cs1.implies(cs2));
+ assertTrue("host should not imply null host", !cs2.implies(cs1));
+ } catch (MalformedURLException e) {
+ fail("Caught MalformedURLException : " + e.toString());
+ }
+ }
+
+ /**
+ * @tests java.security.CodeSource#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String java.security.CodeSource.toString()
+ try {
+ CodeSource cs = new CodeSource(new java.net.URL("file:///test"),
+ (Certificate[]) null);
+ assertEquals("toString did not return expected value.",
+ "(file:/test <no certificates>)", cs.toString());
+ } catch (Exception e) {
+ fail("Caught an exception: " + e);
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,55 @@
+/* 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 tests.api.java.security;
+
+import java.security.DigestException;
+
+public class DigestExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.DigestException#DigestException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.DigestException()
+ DigestException de = new DigestException();
+ assertNull("Exception with no message should yield null message.", de
+ .getMessage());
+ }
+
+ /**
+ * @tests java.security.DigestException#DigestException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.security.DigestException(java.lang.String)
+ DigestException de = new DigestException("Test message");
+ assertEquals("Exception message is incorrect", "Test message", de
+ .getMessage());
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestInputStreamTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestInputStreamTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestInputStreamTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestInputStreamTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,207 @@
+/* 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 tests.api.java.security;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class DigestInputStreamTest extends junit.framework.TestCase {
+
+ ByteArrayInputStream inStream;
+
+ ByteArrayInputStream inStream1;
+
+ MessageDigest digest;
+
+ /**
+ * @tests java.security.DigestInputStream#DigestInputStream(java.io.InputStream,
+ * java.security.MessageDigest)
+ */
+ public void test_ConstructorLjava_io_InputStreamLjava_security_MessageDigest() {
+ // Test for method java.security.DigestInputStream(java.io.InputStream,
+ // java.security.MessageDigest)
+ DigestInputStream dis = new DigestInputStream(inStream, digest);
+ assertNotNull("Constructor returned null instance", dis);
+ }
+
+ /**
+ * @tests java.security.DigestInputStream#getMessageDigest()
+ */
+ public void test_getMessageDigest() {
+ // Test for method java.security.MessageDigest
+ // java.security.DigestInputStream.getMessageDigest()
+ DigestInputStream dis = new DigestInputStream(inStream, digest);
+ assertEquals("getMessageDigest returned a bogus result", digest, dis
+ .getMessageDigest());
+ }
+
+ /**
+ * @tests java.security.DigestInputStream#on(boolean)
+ */
+ public void test_onZ() {
+ // Test for method void java.security.DigestInputStream.on(boolean)
+ try {
+ MessageDigest originalDigest = (MessageDigest) (digest.clone());
+ MessageDigest noChangeDigest = (MessageDigest) (digest.clone());
+ DigestInputStream dis = new DigestInputStream(inStream,
+ noChangeDigest);
+ // turn off processing
+ dis.on(false);
+ // read some data
+ try {
+ int c = dis.read();
+ assertTrue("Stream returned bogus first character. Char was: '"
+ + (char) c + " 'Should have been 'T'", c == 'T');
+ } catch (IOException e) {
+ fail("Stream threw an IOException : " + e);
+ }
+
+ // make sure the digest for the part where it was off has not
+ // changed
+ assertTrue("MessageDigest changed even though processing was off",
+ MessageDigest.isEqual(noChangeDigest.digest(),
+ originalDigest.digest()));
+ MessageDigest changeDigest = (MessageDigest) (digest.clone());
+ dis = new DigestInputStream(inStream, digest);
+
+ // turn on processing
+ dis.on(true);
+ try {
+ int c = dis.read();
+ assertTrue(
+ "Stream returned bogus second character. Char was: '"
+ + (char) c + " 'Should have been 'h'", c == 'h');
+ } catch (IOException e) {
+ fail("Stream threw an IOException : " + e);
+ }
+ // make sure the digest has changed
+ assertTrue("MessageDigest did not change with processing on",
+ !MessageDigest.isEqual(digest.digest(), changeDigest
+ .digest()));
+ } catch (CloneNotSupportedException e) {
+ fail("MessageDigest should support clone : " + e);
+ }
+
+ }
+
+ /**
+ * @tests java.security.DigestInputStream#read()
+ */
+ public void test_read() {
+ // Test for method int java.security.DigestInputStream.read()
+ DigestInputStream dis = new DigestInputStream(inStream, digest);
+
+ // read and compare the data that the inStream has
+ try {
+ int c;
+ while ((c = dis.read()) > -1) {
+ int d = inStream1.read();
+ assertTrue("Stream returned bogus character '" + (char) c
+ + "' should have been '" + (char) d + "'", c == d);
+ }// end while
+ } catch (IOException e) {
+ fail("Stream threw an IOException : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.DigestInputStream#read(byte[], int, int)
+ */
+ public void test_read$BII() {
+ // Test for method int java.security.DigestInputStream.read(byte [],
+ // int, int)
+ DigestInputStream dis = new DigestInputStream(inStream, digest);
+ int bytesToRead = inStream.available();
+ byte buf1[] = new byte[bytesToRead + 5];
+ byte buf2[] = new byte[bytesToRead + 5];
+ // make sure we're actually reading some data
+ assertTrue("No data to read for this test", bytesToRead>0);
+
+ // read and compare the data that the inStream has
+ try {
+ int bytesRead1 = dis.read(buf1, 5, bytesToRead);
+ int bytesRead2 = inStream1.read(buf2, 5, bytesToRead);
+ assertTrue("Didn't read the same from each stream",
+ bytesRead1 == bytesRead2);
+ assertTrue("Didn't read the entire", bytesRead1 == bytesToRead);
+ // compare the arrays
+ boolean same = true;
+ for (int i = 0; i < bytesToRead + 5; i++) {
+ if (buf1[i] != buf2[i]) {
+ same = false;
+ }
+ }// end for
+ assertTrue("Didn't get the same data", same);
+ } catch (IOException e) {
+ fail("Stream threw an IOException : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.DigestInputStream#setMessageDigest(java.security.MessageDigest)
+ */
+ public void test_setMessageDigestLjava_security_MessageDigest() {
+ // Test for method void
+ // java.security.DigestInputStream.setMessageDigest(java.security.MessageDigest)
+ DigestInputStream dis = new DigestInputStream(inStream, null);
+
+ // make sure the digest is null when it's not been set
+ assertNull(
+ "Uninitialised MessageDigest should have been returned as null",
+ dis.getMessageDigest());
+ dis.setMessageDigest(digest);
+ assertEquals("Wrong MessageDigest was returned.", digest, dis
+ .getMessageDigest());
+ }
+
+ /**
+ * @tests java.security.DigestInputStream#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String
+ // java.security.DigestInputStream.toString()
+ DigestInputStream dis = new DigestInputStream(inStream, digest);
+ assertEquals("[Digest Input Stream] SHA-1 Message Digest from "
+ + digest.getProvider().getName() + ", <on>", dis.toString());
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ // create a ByteArrayInputStream to perform digesting on
+ inStream = new ByteArrayInputStream(
+ "This is a test string for digesting".getBytes());
+ inStream1 = new ByteArrayInputStream(
+ "This is a test string for digesting".getBytes());
+ try {
+ digest = MessageDigest.getInstance("SHA-1");
+ } catch (NoSuchAlgorithmException e) {
+ fail("Unable to find SHA-1 algorithim");
+ }
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestOutputStreamTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestOutputStreamTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestOutputStreamTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DigestOutputStreamTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,171 @@
+/* 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 tests.api.java.security;
+
+import java.io.ByteArrayOutputStream;
+import java.security.DigestOutputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class DigestOutputStreamTest extends junit.framework.TestCase {
+ ByteArrayOutputStream output1;
+
+ ByteArrayOutputStream output2;
+
+ MessageDigest digest;
+
+ /**
+ * @tests java.security.DigestOutputStream#DigestOutputStream(java.io.OutputStream,
+ * java.security.MessageDigest)
+ */
+ public void test_ConstructorLjava_io_OutputStreamLjava_security_MessageDigest() {
+ // Test for method
+ // java.security.DigestOutputStream(java.io.OutputStream,
+ // java.security.MessageDigest)
+ assertNotNull("Constructor returned null instance",
+ new DigestOutputStream(output1, digest));
+ }
+
+ /**
+ * @tests java.security.DigestOutputStream#getMessageDigest()
+ */
+ public void test_getMessageDigest() {
+ // Test for method java.security.MessageDigest
+ // java.security.DigestOutputStream.getMessageDigest()
+ DigestOutputStream dos = new DigestOutputStream(output1, digest);
+ assertEquals("getMessageDigest did not return expected result",
+ digest, dos.getMessageDigest());
+ dos = new DigestOutputStream(output1, null);
+ assertNull("getMessageDigest should have returned null", dos
+ .getMessageDigest());
+ }
+
+ /**
+ * @tests java.security.DigestOutputStream#on(boolean)
+ */
+ public void test_onZ() {
+ // Test for method void java.security.DigestOutputStream.on(boolean)
+ try {
+ DigestOutputStream dos = new DigestOutputStream(output1, digest);
+ dos.on(false);
+ byte digestArray[] = { 23, 43, 44 };
+ dos.write(digestArray, 1, 1);
+ byte digestResult[] = dos.getMessageDigest().digest();
+ byte expected[] = { -38, 57, -93, -18, 94, 107, 75, 13, 50, 85,
+ -65, -17, -107, 96, 24, -112, -81, -40, 7, 9 };
+ assertTrue("Digest did not return expected result.",
+ java.util.Arrays.equals(digestResult, expected));
+ // now turn on processing and re-run
+ dos.on(true);
+ dos.write(digestArray, 1, 1);
+ digestResult = dos.getMessageDigest().digest();
+ byte expected1[] = { -87, 121, -17, 16, -52, 111, 106, 54, -33,
+ 107, -118, 50, 51, 7, -18, 59, -78, -30, -37, -100 };
+
+ assertTrue("Digest did not return expected result.",
+ java.util.Arrays.equals(digestResult, expected1));
+ } catch (Exception e) {
+ fail("Caught exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.DigestOutputStream#setMessageDigest(java.security.MessageDigest)
+ */
+ public void test_setMessageDigestLjava_security_MessageDigest() {
+ // Test for method void
+ // java.security.DigestOutputStream.setMessageDigest(java.security.MessageDigest)
+ DigestOutputStream dos = new DigestOutputStream(output1, null);
+ dos.setMessageDigest(digest);
+ assertEquals("getMessageDigest did not return expected result", digest,
+ dos.getMessageDigest());
+ dos.setMessageDigest(null);
+ assertNull("getMessageDigest should have returned null", dos
+ .getMessageDigest());
+ }
+
+ /**
+ * @tests java.security.DigestOutputStream#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String
+ // java.security.DigestOutputStream.toString()
+ DigestOutputStream dos = new DigestOutputStream(output1, digest);
+ assertEquals("[Digest Output Stream] SHA-1 Message Digest from "
+ + digest.getProvider().getName() + ", <on>", dos.toString());
+ }
+
+ /**
+ * @tests java.security.DigestOutputStream#write(byte[], int, int)
+ */
+ public void test_write$BII() {
+ // Test for method void java.security.DigestOutputStream.write(byte [],
+ // int, int)
+ try {
+ DigestOutputStream dos = new DigestOutputStream(output1, digest);
+ byte digestArray[] = { 23, 43, 44 };
+ dos.write(digestArray, 1, 1);
+ byte digestResult[] = dos.getMessageDigest().digest();
+ byte expected[] = { -87, 121, -17, 16, -52, 111, 106, 54, -33, 107,
+ -118, 50, 51, 7, -18, 59, -78, -30, -37, -100 };
+
+ assertTrue("Digest did not return expected result.",
+ java.util.Arrays.equals(digestResult, expected));
+ } catch (Exception e) {
+ fail("Caught exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.DigestOutputStream#write(int)
+ */
+ public void test_writeI() {
+ // Test for method void java.security.DigestOutputStream.write(int)
+ try {
+ DigestOutputStream dos = new DigestOutputStream(output1, digest);
+ dos.write((byte) 43);
+ byte digestResult[] = dos.getMessageDigest().digest();
+ byte expected[] = { -87, 121, -17, 16, -52, 111, 106, 54, -33, 107,
+ -118, 50, 51, 7, -18, 59, -78, -30, -37, -100 };
+
+ assertTrue("Digest did not return expected result.",
+ java.util.Arrays.equals(digestResult, expected));
+ } catch (Exception e) {
+ fail("Caught exception : " + e);
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ output1 = new ByteArrayOutputStream();
+ try {
+ digest = MessageDigest.getInstance("SHA");
+ } catch (NoSuchAlgorithmException e) {
+ fail("Unable to find SHA algorithim");
+ }
+
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DomainCombinerTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DomainCombinerTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DomainCombinerTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/DomainCombinerTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,118 @@
+/* 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 tests.api.java.security;
+
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.AllPermission;
+import java.security.CodeSource;
+import java.security.DomainCombiner;
+import java.security.PermissionCollection;
+import java.security.Permissions;
+import java.security.PrivilegedAction;
+import java.security.ProtectionDomain;
+import java.security.SecurityPermission;
+import java.security.cert.Certificate;
+
+public class DomainCombinerTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.DomainCombiner#combine(java.security.ProtectionDomain[],
+ * java.security.ProtectionDomain[])
+ */
+ public void test_combine$Ljava_security_ProtectionDomain$Ljava_security_ProtectionDomain() {
+ final boolean[] calledDomainCombiner = new boolean[] { false, false };
+
+ class MyCombiner implements DomainCombiner {
+ int i;
+
+ MyCombiner(int i) {
+ this.i = i;
+ }
+
+ public ProtectionDomain[] combine(
+ ProtectionDomain[] executionDomains,
+ ProtectionDomain[] parentDomains) {
+ calledDomainCombiner[i] = true;
+ PermissionCollection pc = new Permissions();
+ pc.add(new AllPermission());
+ ProtectionDomain pd;
+ // if run with the system classloader then there will be no
+ // execution domains
+ if (executionDomains.length > 0) {
+ pd = new ProtectionDomain(executionDomains[0]
+ .getCodeSource(), pc);
+ } else {
+ pd = new ProtectionDomain(parentDomains[0].getCodeSource(),
+ pc);
+ }
+ return new ProtectionDomain[] { pd };
+ }
+ }
+
+ ProtectionDomain[] domains = new ProtectionDomain[] { new ProtectionDomain(
+ new CodeSource(null, (Certificate[]) null), new Permissions()) };
+
+ final AccessControlContext parent = new AccessControlContext(domains);
+ final AccessControlContext c0 = new AccessControlContext(parent,
+ new MyCombiner(0));
+ final AccessControlContext c1 = new AccessControlContext(parent,
+ new MyCombiner(1));
+ System.setSecurityManager(new SecurityManager());
+ try {
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ try {
+ // AccessController.getContext();
+ AccessController
+ .checkPermission(new SecurityPermission(
+ "MyTest"));
+
+ AccessController.doPrivileged(new PrivilegedAction() {
+ public Object run() {
+ try {
+ AccessController
+ .checkPermission(new SecurityPermission(
+ "MyTest"));
+ return new Integer(1);
+ } catch (SecurityException ex) {
+ calledDomainCombiner[1] = false;
+ return new Integer(-1);
+ }
+ }
+ }, c1);
+ return new Integer(10);
+ } catch (SecurityException ex) {
+ calledDomainCombiner[0] = false;
+ return new Integer(-10);
+ }
+ }
+ }, c0);
+ assertTrue("Failed to combine domains for security permission",
+ calledDomainCombiner[0]);
+ assertTrue("Failed to combine domains for security permission",
+ calledDomainCombiner[1]);
+ } finally {
+ System.setSecurityManager(null);
+ }
+ }
+
+ protected void setUp() {
+ }
+
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/GeneralSecurityExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/GeneralSecurityExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/GeneralSecurityExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/GeneralSecurityExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,60 @@
+/* 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 tests.api.java.security;
+
+import java.security.GeneralSecurityException;
+
+public class GeneralSecurityExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.GeneralSecurityException#GeneralSecurityException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.GeneralSecurityException()
+ GeneralSecurityException e = new GeneralSecurityException();
+ assertNotNull("Constructor returned null instance", e);
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals("java.security.GeneralSecurityException"));
+ }
+
+ /**
+ * @tests java.security.GeneralSecurityException#GeneralSecurityException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method
+ // java.security.GeneralSecurityException(java.lang.String)
+ GeneralSecurityException e = new GeneralSecurityException(
+ "test message");
+ assertNotNull("Constructor returned null instance", e);
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals(
+ "java.security.GeneralSecurityException: test message"));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/IdentityScopeTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/IdentityScopeTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/IdentityScopeTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/IdentityScopeTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,310 @@
+/* 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 tests.api.java.security;
+
+import java.security.Identity;
+import java.security.IdentityScope;
+import java.security.KeyManagementException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.Principal;
+import java.security.PublicKey;
+import java.util.Enumeration;
+import java.util.Hashtable;
+
+import tests.api.java.security.IdentityTest.IdentitySubclass;
+
+public class IdentityScopeTest extends junit.framework.TestCase {
+
+ static PublicKey pubKey;
+
+ public static class IdentityScopeSubclass extends IdentityScope {
+ Hashtable identities;
+
+ public IdentityScopeSubclass(String name, PublicKey pk) {
+ super(name);
+ try {
+ setPublicKey(pk);
+ } catch (KeyManagementException e) {
+ }
+ identities = new Hashtable();
+ }
+
+ public IdentityScopeSubclass() {
+ super();
+ identities = new Hashtable();
+ }
+
+ public IdentityScopeSubclass(String name) {
+ super(name);
+ identities = new Hashtable();
+ }
+
+ public IdentityScopeSubclass(String name, IdentityScope scope)
+ throws KeyManagementException {
+ super(name, scope);
+ identities = new Hashtable();
+ }
+
+ public int size() {
+ return identities.size();
+ }
+
+ public Identity getIdentity(String name) {
+ Enumeration en = identities();
+ while (en.hasMoreElements()) {
+ Identity current = (Identity) en.nextElement();
+ if (current.getName().equals(name))
+ return current;
+ }
+ return null;
+ }
+
+ public Identity getIdentity(PublicKey pk) {
+ Enumeration en = identities();
+ while (en.hasMoreElements()) {
+ Identity current = (Identity) en.nextElement();
+ if (current.getPublicKey() == pk)
+ return current;
+ }
+ return null;
+ }
+
+ public Enumeration identities() {
+ return identities.elements();
+ }
+
+ public void addIdentity(Identity id) throws KeyManagementException {
+ if (identities.containsKey(id))
+ throw new KeyManagementException(
+ "This Identity is already contained in the scope");
+ if (getIdentity(id.getPublicKey()) != null)
+ throw new KeyManagementException(
+ "This Identity's public key already exists in the scope");
+ identities.put(id, id);
+ }
+
+ public void removeIdentity(Identity id) throws KeyManagementException {
+ if (!identities.containsKey(id))
+ throw new KeyManagementException(
+ "This Identity is not contained in the scope");
+ identities.remove(id);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#IdentityScope()
+ */
+ public void test_Constructor() {
+ try {
+ assertNotNull(new IdentityScopeSubclass());
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#IdentityScope(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ try {
+ assertNotNull(new IdentityScopeSubclass("test"));
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#IdentityScope(java.lang.String,
+ * java.security.IdentityScope)
+ */
+ public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() {
+ try {
+ assertNotNull(new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass()));
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#addIdentity(java.security.Identity)
+ */
+ public void test_addIdentityLjava_security_Identity() {
+ try {
+ IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass());
+ Identity id = new IdentitySubclass("id1");
+ id.setPublicKey(pubKey);
+ sub.addIdentity(id);
+ try {
+ Identity id2 = new IdentitySubclass("id2");
+ id2.setPublicKey(pubKey);
+ sub.addIdentity(id2);
+ fail("KeyManagementException should have been thrown");
+ } catch (KeyManagementException e) {
+ // Expected
+ }
+ } catch (Exception e) {
+ fail("Caught an unexpected exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#removeIdentity(java.security.Identity)
+ */
+ public void test_removeIdentityLjava_security_Identity() {
+ try {
+ IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass());
+ Identity id = new IdentitySubclass();
+ id.setPublicKey(pubKey);
+ sub.addIdentity(id);
+ sub.removeIdentity(id);
+ try {
+ sub.removeIdentity(id);
+ fail("KeyManagementException should have been thrown");
+ } catch (KeyManagementException e) {
+ // expected
+ }
+ } catch (Exception e) {
+ fail("Caught an unexpected exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#identities()
+ */
+ public void test_identities() {
+ try {
+ IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass());
+ Identity id = new IdentitySubclass();
+ id.setPublicKey(pubKey);
+ sub.addIdentity(id);
+ Enumeration en = sub.identities();
+ assertTrue("Wrong object contained in identities", en.nextElement()
+ .equals(id));
+ assertTrue("Contains too many elements", !en.hasMoreElements());
+ } catch (Exception e) {
+ fail("Caught unexpected exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#getIdentity(java.security.Principal)
+ */
+ public void test_getIdentityLjava_security_Principal() {
+ try {
+ Identity id = new IdentitySubclass("principal name");
+ id.setPublicKey(pubKey);
+ IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass());
+ sub.addIdentity(id);
+ Identity returnedId = sub.getIdentity((Principal) id);
+ assertEquals("Returned Identity not the same as the added one", id,
+ returnedId);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#getIdentity(java.security.PublicKey)
+ */
+ public void test_getIdentityLjava_security_PublicKey() {
+ try {
+ IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass());
+ Identity id = new IdentitySubclass();
+ id.setPublicKey(pubKey);
+ sub.addIdentity(id);
+ Identity returnedId = sub.getIdentity(pubKey);
+ assertEquals("Returned Identity not the same as the added one", id,
+ returnedId);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#getIdentity(java.lang.String)
+ */
+ public void test_getIdentityLjava_lang_String() {
+ try {
+ IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass());
+ Identity id = new IdentitySubclass("test");
+ id.setPublicKey(pubKey);
+ sub.addIdentity(id);
+ Identity returnedId = sub.getIdentity("test");
+ assertEquals("Returned Identity not the same as the added one", id,
+ returnedId);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#size()
+ */
+ public void test_size() {
+ try {
+ IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass());
+ Identity id = new IdentitySubclass();
+ id.setPublicKey(pubKey);
+ sub.addIdentity(id);
+ assertEquals("Wrong size", 1, sub.size());
+ } catch (Exception e) {
+ fail("Caught unexpected exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.IdentityScope#toString()
+ */
+ public void test_toString() {
+ try {
+ IdentityScopeSubclass sub = new IdentityScopeSubclass("test",
+ new IdentityScopeSubclass());
+ Identity id = new IdentitySubclass();
+ id.setPublicKey(pubKey);
+ sub.addIdentity(id);
+ assertNotNull("toString returned a null", sub.toString());
+ assertTrue("Not a valid String ", sub.toString().length() > 0);
+ } catch (Exception e) {
+ fail("Caught unexpected exception : " + e);
+ }
+ }
+
+ protected void setUp() {
+
+ fail("Set up hangs - requires a full math implementation ??");
+
+ try {
+ KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
+ KeyPair pair = gen.genKeyPair();
+ pubKey = pair.getPublic();
+ } catch (NoSuchAlgorithmException e) {
+ fail("Unable to find DSA algorithm");
+ }
+ }
+
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/IdentityTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/IdentityTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/IdentityTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/IdentityTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,382 @@
+/* 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 tests.api.java.security;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.Identity;
+import java.security.IdentityScope;
+import java.security.KeyManagementException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.Principal;
+import java.security.PublicKey;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+
+import tests.api.java.security.IdentityScopeTest.IdentityScopeSubclass;
+
+public class IdentityTest extends junit.framework.TestCase {
+
+ public static class CertificateImpl implements java.security.Certificate {
+
+ X509Certificate cert;
+
+ public CertificateImpl(X509Certificate cert) {
+ this.cert = cert;
+ }
+
+ public Principal getGuarantor() {
+ return cert.getIssuerDN();
+ }
+
+ public void encode(OutputStream out) {
+ }
+
+ public void decode(InputStream in) {
+ }
+
+ public String toString() {
+ return "";
+ }
+
+ public String toString(boolean b) {
+ return "";
+ }
+
+ public String getFormat() {
+ return cert.getType();
+ }
+
+ public Principal getPrincipal() {
+ return cert.getSubjectDN();
+ }
+
+ public PublicKey getPublicKey() {
+ return cert.getPublicKey();
+ }
+ }
+
+ String certificate = "-----BEGIN CERTIFICATE-----\n"
+ + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
+ + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
+ + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
+ + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
+ + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
+ + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
+ + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
+ + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
+ + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
+ + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
+ + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
+ + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
+ + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
+ + "-----END CERTIFICATE-----\n";
+
+ ByteArrayInputStream certArray = new ByteArrayInputStream(certificate
+ .getBytes());
+
+ String certificate2 = "-----BEGIN CERTIFICATE-----\n"
+ + "MIICZzCCAdCgAwIBAgIBGzANBgkqhkiG9w0BAQUFADBhMQswCQYDVQQGEwJVUzEY\n"
+ + "MBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsT\n"
+ + "A1BLSTEcMBoGA1UEAxMTRG9EIFBLSSBNZWQgUm9vdCBDQTAeFw05ODA4MDMyMjAy\n"
+ + "MjlaFw0wODA4MDQyMjAyMjlaMGExCzAJBgNVBAYTAlVTMRgwFgYDVQQKEw9VLlMu\n"
+ + "IEdvdmVybm1lbnQxDDAKBgNVBAsTA0RvRDEMMAoGA1UECxMDUEtJMRwwGgYDVQQD\n"
+ + "ExNEb0QgUEtJIE1lZCBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n"
+ + "gQDbrM/J9FrJSX+zxFUbsI9Vw5QbguVBIa95rwW/0M8+sM0r5gd+DY6iubm6wnXk\n"
+ + "CSvbfQlFEDSKr4WYeeGp+d9WlDnQdtDFLdA45tCi5SHjnW+hGAmZnld0rz6wQekF\n"
+ + "5xQaa5A6wjhMlLOjbh27zyscrorMJ1O5FBOWnEHcRv6xqQIDAQABoy8wLTAdBgNV\n"
+ + "HQ4EFgQUVrmYR6m9701cHQ3r5kXyG7zsCN0wDAYDVR0TBAUwAwEB/zANBgkqhkiG\n"
+ + "9w0BAQUFAAOBgQDVX1Y0YqC7vekeZjVxtyuC8Mnxbrz6D109AX07LEIRzNYzwZ0w\n"
+ + "MTImSp9sEzWW+3FueBIU7AxGys2O7X0qmN3zgszPfSiocBuQuXIYQctJhKjF5KVc\n"
+ + "VGQRYYlt+myhl2vy6yPzEVCjiKwMEb1Spu0irCf+lFW2hsdjvmSQMtZvOw==\n"
+ + "-----END CERTIFICATE-----\n";
+
+ ByteArrayInputStream certArray2 = new ByteArrayInputStream(certificate2
+ .getBytes());
+
+ static PublicKey pubKey;
+
+ public static class IdentitySubclass extends Identity {
+ public IdentitySubclass() {
+ super();
+ }
+
+ public IdentitySubclass(String name) {
+ super(name);
+ }
+
+ public IdentitySubclass(String name, IdentityScope scope)
+ throws KeyManagementException {
+ super(name, scope);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#Identity()
+ */
+ public void test_Constructor() {
+ try {
+ assertNotNull(new IdentitySubclass());
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#Identity(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ try {
+ assertNotNull(new IdentitySubclass("test"));
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#Identity(java.lang.String,
+ * java.security.IdentityScope)
+ */
+ public void test_ConstructorLjava_lang_StringLjava_security_IdentityScope() {
+ try {
+ assertNotNull(new IdentitySubclass("test",
+ new IdentityScopeSubclass()));
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#getScope()
+ */
+ public void test_getScope() {
+ try {
+ IdentityScope scope = new IdentityScopeSubclass();
+ IdentitySubclass sub = new IdentitySubclass("test", scope);
+ IdentityScope returnedScope = sub.getScope();
+ assertEquals("Wrong Scope returned", scope, returnedScope);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#getPublicKey()
+ */
+ public void test_getPublicKey() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test",
+ new IdentityScopeSubclass());
+ sub.setPublicKey(pubKey);
+ PublicKey returnedPubKey = sub.getPublicKey();
+ assertEquals("Wrong PublicKey returned", pubKey, returnedPubKey);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#getName()
+ */
+ public void test_getName() {
+ try {
+ String name = "test";
+ IdentitySubclass sub = new IdentitySubclass(name,
+ new IdentityScopeSubclass());
+ assertEquals("Wrong Name returned", name, sub.getName());
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#getInfo()
+ */
+ public void test_getInfo() {
+ try {
+ String info = "This is the general information.";
+ IdentitySubclass sub = new IdentitySubclass("test",
+ new IdentityScopeSubclass());
+ sub.setInfo(info);
+ assertEquals("Wrong Info returned", info, sub.getInfo());
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#certificates()
+ */
+ public void test_certificates() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test",
+ new IdentityScopeSubclass());
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[1];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ sub.setPublicKey(cert[0].getPublicKey());
+ CertificateImpl certImpl = new CertificateImpl(cert[0]);
+ sub.addCertificate(certImpl);
+ java.security.Certificate[] certs = sub.certificates();
+ assertEquals("Certificate not contained in the identity",
+ certs[0], certImpl);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#addCertificate(java.security.Certificate)
+ */
+ public void test_addCertificateLjava_security_Certificate() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test",
+ new IdentityScopeSubclass());
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[1];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ sub.setPublicKey(cert[0].getPublicKey());
+ CertificateImpl certImpl = new CertificateImpl(cert[0]);
+ sub.addCertificate(certImpl);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#removeCertificate(java.security.Certificate)
+ */
+ public void test_removeCertificateLjava_security_Certificate() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test",
+ new IdentityScopeSubclass());
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[1];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ sub.setPublicKey(cert[0].getPublicKey());
+ CertificateImpl certImpl = new CertificateImpl(cert[0]);
+ sub.addCertificate(certImpl);
+ sub.removeCertificate(certImpl);
+ java.security.Certificate[] certs = sub.certificates();
+ assertEquals("Certificate not removed", 0, certs.length);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test",
+ new IdentityScopeSubclass());
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[1];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ sub.setPublicKey(cert[0].getPublicKey());
+ CertificateImpl certImpl = new CertificateImpl(cert[0]);
+ sub.addCertificate(certImpl);
+ IdentitySubclass sub2 = new IdentitySubclass("test",
+ new IdentityScopeSubclass());
+ assertEquals("the two Identity objects are not equal", sub2, sub);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#identityEquals(java.security.Identity)
+ */
+ public void test_identityEqualsLjava_security_Identity() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test", null);
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[1];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ sub.setPublicKey(cert[0].getPublicKey());
+ CertificateImpl certImpl = new CertificateImpl(cert[0]);
+ sub.addCertificate(certImpl);
+ IdentitySubclass sub2 = new IdentitySubclass("test", null);
+ sub2.setPublicKey(cert[0].getPublicKey());
+ assertEquals("the two Identity objects are not identity-equal",
+ sub2, sub);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#toString()
+ */
+ public void test_toString() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test", null);
+ assertNotNull(sub.toString());
+ assertTrue("The String returned is not valid", sub.toString()
+ .length() > 0);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#toString(boolean)
+ */
+ public void test_toStringZ() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test", null);
+ assertNotNull(sub.toString(true));
+ assertTrue("The String returned is not valid", sub.toString(true)
+ .length() > 0);
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Identity#hashCode()
+ */
+ public void test_hashCode() {
+ try {
+ IdentitySubclass sub = new IdentitySubclass("test", null);
+ IdentitySubclass sub2 = new IdentitySubclass("test", null);
+ assertEquals("The 2 hash codes are not equal", sub.hashCode(), sub2
+ .hashCode());
+ } catch (Exception e) {
+ fail("Caught an exception : " + e);
+ }
+ }
+
+ protected void setUp() {
+
+ fail("Set up hangs - requires a full math implementation ??");
+
+ try {
+ KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
+ KeyPair pair = gen.genKeyPair();
+ pubKey = pair.getPublic();
+ } catch (NoSuchAlgorithmException e) {
+ fail("Unable to find DSA algorithm");
+ }
+ }
+
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidAlgorithmParameterExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidAlgorithmParameterExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidAlgorithmParameterExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidAlgorithmParameterExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,63 @@
+/* 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 tests.api.java.security;
+
+import java.security.InvalidAlgorithmParameterException;
+
+public class InvalidAlgorithmParameterExceptionTest extends
+ junit.framework.TestCase {
+
+ /**
+ * @tests java.security.InvalidAlgorithmParameterException#InvalidAlgorithmParameterException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.InvalidAlgorithmParameterException()
+ InvalidAlgorithmParameterException e = new InvalidAlgorithmParameterException();
+ assertNotNull("Constructor returned null instance", e);
+ assertEquals("Failed toString test for constructed instance",
+ "java.security.InvalidAlgorithmParameterException", e
+ .toString());
+ }
+
+ /**
+ * @tests java.security.InvalidAlgorithmParameterException#InvalidAlgorithmParameterException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method
+ // java.security.InvalidAlgorithmParameterException(java.lang.String)
+ InvalidAlgorithmParameterException e = new InvalidAlgorithmParameterException(
+ "test message");
+ assertNotNull("Constructor returned null instance", e);
+ assertEquals(
+ "Failed toString test for constructed instance",
+ "java.security.InvalidAlgorithmParameterException: test message",
+ e.toString());
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidKeyExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidKeyExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidKeyExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidKeyExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,58 @@
+/* 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 tests.api.java.security;
+
+import java.security.InvalidKeyException;
+
+public class InvalidKeyExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.InvalidKeyException#InvalidKeyException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.InvalidKeyException()
+ InvalidKeyException e = new InvalidKeyException();
+ assertNotNull("Constructor returned a null", e);
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals("java.security.InvalidKeyException"));
+ }
+
+ /**
+ * @tests java.security.InvalidKeyException#InvalidKeyException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.security.InvalidKeyException(java.lang.String)
+ InvalidKeyException e = new InvalidKeyException("test message");
+ assertNotNull("Constructor returned a null", e);
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals(
+ "java.security.InvalidKeyException: test message"));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidParameterExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidParameterExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidParameterExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/InvalidParameterExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,61 @@
+/* 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 tests.api.java.security;
+
+import java.security.InvalidParameterException;
+
+public class InvalidParameterExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.InvalidParameterException#InvalidParameterException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.InvalidParameterException()
+ InvalidParameterException e = new InvalidParameterException();
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals("java.security.InvalidParameterException"));
+ }
+
+ /**
+ * @tests java.security.InvalidParameterException#InvalidParameterException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method
+ // java.security.InvalidParameterException(java.lang.String)
+ InvalidParameterException e = new InvalidParameterException(
+ "test message");
+ assertTrue(
+ "Failed toString test for constructed instance",
+ e
+ .toString()
+ .equals(
+ "java.security.InvalidParameterException: test message"));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,55 @@
+/* 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 tests.api.java.security;
+
+import java.security.KeyException;
+
+public class KeyExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.KeyException#KeyException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.KeyException()
+ KeyException e = new KeyException();
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals("java.security.KeyException"));
+ }
+
+ /**
+ * @tests java.security.KeyException#KeyException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.security.KeyException(java.lang.String)
+ KeyException e = new KeyException("test message");
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals("java.security.KeyException: test message"));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyFactoryTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyFactoryTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyFactoryTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyFactoryTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,410 @@
+/* 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 tests.api.java.security;
+
+import java.security.InvalidKeyException;
+import java.security.KeyFactory;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.PrivateKey;
+import java.security.Provider;
+import java.security.PublicKey;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.Vector;
+
+public class KeyFactoryTest extends junit.framework.TestCase {
+
+ private static final String KEYFACTORY_ID = "KeyFactory.";
+
+ private String[] keyfactAlgs = null;
+
+ private String providerName = null;
+
+ static class KeepAlive extends Thread {
+ int sleepTime, iterations;
+
+ public KeepAlive(int sleepTime, int iterations) {
+ this.sleepTime = sleepTime;
+ this.iterations = iterations;
+ }
+
+ public void run() {
+ synchronized (this) {
+ this.notify();
+ }
+ for (int i = 0; i < iterations; i++) {
+ try {
+ Thread.sleep(sleepTime);
+ System.out.print("[KA]");
+ } catch (InterruptedException e) {
+ System.out.print("[I]");
+ break;
+ }
+ }
+ }
+ }
+
+ private KeepAlive createKeepAlive(String alg) {
+ if (alg.equals("RSA")) {
+ // 32 minutes
+ KeepAlive keepalive = new KeepAlive(240000, 8);
+ synchronized (keepalive) {
+ keepalive.start();
+ try {
+ keepalive.wait();
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ }
+ return keepalive;
+ }
+ return null;
+ }
+
+ /**
+ * @tests java.security.KeyFactory#generatePrivate(java.security.spec.KeySpec)
+ */
+ public void test_generatePrivateLjava_security_spec_KeySpec() {
+ // Test for method java.security.PrivateKey
+ // java.security.KeyFactory.generatePrivate(java.security.spec.KeySpec)
+ for (int i = 0; i < keyfactAlgs.length; i++) {
+ try {
+ KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i],
+ providerName);
+ KeyPairGenerator keyGen = KeyPairGenerator
+ .getInstance(keyfactAlgs[i]);
+ SecureRandom random = new SecureRandom(); // We don't use
+ // getInstance
+ keyGen.initialize(1024, random);
+ KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
+ KeyPair keys = keyGen.generateKeyPair();
+ if (keepalive != null) {
+ keepalive.interrupt();
+ }
+
+ KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(),
+ getPrivateKeySpecClass(keyfactAlgs[i]));
+ PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
+ boolean samePrivate = Arrays.equals(keys.getPrivate()
+ .getEncoded(), privateKey.getEncoded());
+ assertTrue(
+ "generatePrivate generated different key for algorithm "
+ + keyfactAlgs[i], samePrivate);
+ fact.generatePrivate(new PKCS8EncodedKeySpec(keys.getPrivate()
+ .getEncoded()));
+ } catch (InvalidKeySpecException e) {
+ fail("invalid key spec for algorithm " + keyfactAlgs[i]);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + keyfactAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.KeyFactory#generatePublic(java.security.spec.KeySpec)
+ */
+ public void test_generatePublicLjava_security_spec_KeySpec() {
+ // Test for method java.security.PublicKey
+ // java.security.KeyFactory.generatePublic(java.security.spec.KeySpec)
+ for (int i = 0; i < keyfactAlgs.length; i++) {
+ try {
+ KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i],
+ providerName);
+ KeyPairGenerator keyGen = KeyPairGenerator
+ .getInstance(keyfactAlgs[i]);
+ // We don't use getInstance
+ SecureRandom random = new SecureRandom();
+ keyGen.initialize(1024, random);
+ KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
+ KeyPair keys = keyGen.generateKeyPair();
+ if (keepalive != null) {
+ keepalive.interrupt();
+ }
+ KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(),
+ getPublicKeySpecClass(keyfactAlgs[i]));
+ PublicKey publicKey = fact.generatePublic(publicKeySpec);
+ boolean samePublic = Arrays.equals(keys.getPublic()
+ .getEncoded(), publicKey.getEncoded());
+ assertTrue(
+ "generatePublic generated different key for algorithm "
+ + keyfactAlgs[i], samePublic);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + keyfactAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ } catch (InvalidKeySpecException e) {
+ fail("invalid key spec for algorithm " + keyfactAlgs[i]);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.KeyFactory#getAlgorithm()
+ */
+ public void test_getAlgorithm() {
+ // Test for method java.lang.String
+ // java.security.KeyFactory.getAlgorithm()
+ for (int i = 0; i < keyfactAlgs.length; i++) {
+ try {
+ KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i],
+ providerName);
+ assertTrue("getAlgorithm ok for algorithm " + keyfactAlgs[i],
+ fact.getAlgorithm().equals(keyfactAlgs[i]));
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + keyfactAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ }
+ }// end for
+ }
+
+ /**
+ * @tests java.security.KeyFactory#getInstance(java.lang.String)
+ */
+ public void test_getInstanceLjava_lang_String() {
+ // Test for method java.security.KeyFactory
+ // java.security.KeyFactory.getInstance(java.lang.String)
+ for (int i = 0; i < keyfactAlgs.length; i++) {
+ try {
+ assertNotNull(KeyFactory.getInstance(keyfactAlgs[i]));
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + keyfactAlgs[i]);
+ }
+ }// end for
+ }
+
+ /**
+ * @tests java.security.KeyFactory#getInstance(java.lang.String,
+ * java.lang.String)
+ */
+ public void test_getInstanceLjava_lang_StringLjava_lang_String() {
+
+ // Test1: Test for method java.security.KeyFactory
+ // java.security.KeyFactory.getInstance(java.lang.String,
+ // java.lang.String)
+ try {
+ Provider[] providers = Security.getProviders("KeyFactory.DSA");
+ if (providers != null) {
+ for (int i = 0; i < providers.length; i++) {
+ KeyFactory.getInstance("DSA", providers[i].getName());
+ }// end for
+ } else {
+ fail("No providers support KeyFactory.DSA");
+ }
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find the provider");
+ }
+
+ // Test2: Test with null provider name
+ try {
+ KeyFactory kf = KeyFactory.getInstance("DSA", (String) null);
+ } catch (IllegalArgumentException e) {
+ // Expected
+ } catch (Exception e) {
+ fail("Expected IllegalArgumentException, got " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyFactory#getKeySpec(java.security.Key,
+ * java.lang.Class)
+ */
+ public void test_getKeySpecLjava_security_KeyLjava_lang_Class() {
+ // Test for method java.security.spec.KeySpec
+ // java.security.KeyFactory.getKeySpec(java.security.Key,
+ // java.lang.Class)
+ for (int i = 0; i < keyfactAlgs.length; i++) {
+ try {
+ KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i],
+ providerName);
+ KeyPairGenerator keyGen = KeyPairGenerator
+ .getInstance(keyfactAlgs[i]);
+
+ // We don't use getInstance
+ SecureRandom random = new SecureRandom();
+ keyGen.initialize(1024, random);
+ KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
+ KeyPair keys = keyGen.generateKeyPair();
+ if (keepalive != null) {
+ keepalive.interrupt();
+ }
+ KeySpec privateKeySpec = fact.getKeySpec(keys.getPrivate(),
+ getPrivateKeySpecClass(keyfactAlgs[i]));
+ KeySpec publicKeySpec = fact.getKeySpec(keys.getPublic(),
+ getPublicKeySpecClass(keyfactAlgs[i]));
+ PrivateKey privateKey = fact.generatePrivate(privateKeySpec);
+ PublicKey publicKey = fact.generatePublic(publicKeySpec);
+ boolean samePublic = Arrays.equals(keys.getPublic()
+ .getEncoded(), publicKey.getEncoded());
+ boolean samePrivate = Arrays.equals(keys.getPrivate()
+ .getEncoded(), privateKey.getEncoded());
+ assertTrue(
+ "generatePrivate generated different key for algorithm "
+ + keyfactAlgs[i], samePrivate);
+ assertTrue(
+ "generatePublic generated different key for algorithm "
+ + keyfactAlgs[i], samePublic);
+ KeySpec encodedSpec = fact.getKeySpec(keys.getPublic(),
+ X509EncodedKeySpec.class);
+ assertTrue("improper key spec for encoded public key",
+ encodedSpec.getClass().equals(X509EncodedKeySpec.class));
+ encodedSpec = fact.getKeySpec(keys.getPrivate(),
+ PKCS8EncodedKeySpec.class);
+ assertTrue("improper key spec for encoded private key",
+ encodedSpec.getClass()
+ .equals(PKCS8EncodedKeySpec.class));
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + keyfactAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ } catch (InvalidKeySpecException e) {
+ fail("invalid key spec for algorithm " + keyfactAlgs[i]);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.KeyFactory#getProvider()
+ */
+ public void test_getProvider() {
+ // Test for method java.security.Provider
+ // java.security.KeyFactory.getProvider()
+ for (int i = 0; i < keyfactAlgs.length; i++) {
+ try {
+ KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i]);
+ Provider p = fact.getProvider();
+ assertNotNull("provider is null for algorithm "
+ + keyfactAlgs[i], p);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + keyfactAlgs[i]);
+ }
+ }// end for
+ }
+
+ /**
+ * @tests java.security.KeyFactory#translateKey(java.security.Key)
+ */
+ public void test_translateKeyLjava_security_Key() {
+ // Test for method java.security.Key
+ // java.security.KeyFactory.translateKey(java.security.Key)
+ for (int i = 0; i < keyfactAlgs.length; i++) {
+ try {
+ KeyFactory fact = KeyFactory.getInstance(keyfactAlgs[i],
+ providerName);
+ KeyPairGenerator keyGen = KeyPairGenerator
+ .getInstance(keyfactAlgs[i]);
+
+ // We don't use getInstance
+ SecureRandom random = new SecureRandom();
+ keyGen.initialize(1024, random);
+ KeepAlive keepalive = createKeepAlive(keyfactAlgs[i]);
+ KeyPair keys = keyGen.generateKeyPair();
+ if (keepalive != null) {
+ keepalive.interrupt();
+ }
+ fact.translateKey(keys.getPrivate());
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + keyfactAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ } catch (InvalidKeyException e) {
+ fail("generatePublic did not generate right spec for algorithm "
+ + keyfactAlgs[i]);
+ }
+ }
+ }
+
+ protected void setUp() {
+ if (keyfactAlgs == null) {
+ Provider[] providers = Security.getProviders();
+ if (providers == null) {
+ fail("No providers found");
+ }
+ // Arbitrarily uUse the first provider that supports
+ // KeyFactory algorithms
+ providerName = providers[0].getName();
+ keyfactAlgs = getKeyFactoryAlgorithms(providerName);
+ }
+ }
+
+ /*
+ * Returns the key algorithms that the given provider supports.
+ */
+ private String[] getKeyFactoryAlgorithms(String providerName) {
+ Vector algs = new Vector();
+
+ Provider provider = Security.getProvider(providerName);
+ if (provider == null)
+ return new String[0];
+ Enumeration e = provider.keys();
+ while (e.hasMoreElements()) {
+ String algorithm = (String) e.nextElement();
+ if (algorithm.startsWith(KEYFACTORY_ID)) {
+ algs.addElement(algorithm.substring(KEYFACTORY_ID.length()));
+ }
+ }
+
+ return (String[]) algs.toArray(new String[algs.size()]);
+ }
+
+ /**
+ * Returns the public key spec class for a given algorithm, or null if it is
+ * not known.
+ */
+ private Class getPrivateKeySpecClass(String algName) {
+ if (algName.equals("RSA")) {
+ return java.security.spec.RSAPrivateCrtKeySpec.class;
+ }
+ if (algName.equals("DSA")) {
+ return java.security.spec.DSAPrivateKeySpec.class;
+ }
+ return null;
+ }
+
+ /**
+ * Returns the private key spec class for a given algorithm, or null if it
+ * is not known.
+ */
+ private Class getPublicKeySpecClass(String algName) {
+ if (algName.equals("RSA")) {
+ return java.security.spec.RSAPublicKeySpec.class;
+ }
+ if (algName.equals("DSA")) {
+ return java.security.spec.DSAPublicKeySpec.class;
+ }
+ return null;
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
\ No newline at end of file