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 [3/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/KeyManagementExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyManagementExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyManagementExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyManagementExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,57 @@
+/* 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.KeyManagementException;
+
+public class KeyManagementExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.KeyManagementException#KeyManagementException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.KeyManagementException()
+ KeyManagementException e = new KeyManagementException();
+ assertEquals("Failed toString test for constructed instance",
+ "java.security.KeyManagementException", e.toString());
+ }
+
+ /**
+ * @tests java.security.KeyManagementException#KeyManagementException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method
+ // java.security.KeyManagementException(java.lang.String)
+ KeyManagementException e = new KeyManagementException("test message");
+ assertEquals("Failed toString test for constructed instance",
+ "java.security.KeyManagementException: 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/KeyPairGeneratorTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyPairGeneratorTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyPairGeneratorTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyPairGeneratorTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,180 @@
+/* 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.math.BigInteger;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.SecureRandom;
+import java.security.Security;
+import java.security.spec.DSAParameterSpec;
+
+public class KeyPairGeneratorTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.KeyPairGenerator#genKeyPair()
+ */
+ public void test_genKeyPair() {
+
+ fail("Test hangs - requires proper math implementation ?");
+
+ try {
+ KeyPairGenerator gen = KeyPairGenerator.getInstance("DSA");
+ gen.initialize(1024);
+ KeyPair pair = gen.genKeyPair();
+ assertNotNull("KeyPair is null", pair);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ }
+ }
+
+ /**
+ * @tests java.security.KeyPairGenerator#getAlgorithm()
+ */
+ public void test_getAlgorithm() {
+ try {
+ String alg = KeyPairGenerator.getInstance("DSA").getAlgorithm();
+ assertEquals("getAlgorithm returned enexpected value", "DSA", alg);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ }
+ }
+
+ /**
+ * @tests java.security.KeyPairGenerator#getInstance(java.lang.String)
+ */
+ public void test_getInstanceLjava_lang_String() {
+ try {
+ KeyPairGenerator.getInstance("DSA");
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ } catch (Exception e) {
+ fail("Caught unexpected exception : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyPairGenerator#getInstance(java.lang.String,
+ * java.lang.String)
+ */
+ public void test_getInstanceLjava_lang_StringLjava_lang_String() {
+ // Test1: Test with named provider
+ try {
+ Provider[] providers = Security
+ .getProviders("KeyPairGenerator.DSA");
+ if (providers == null) {
+ fail("No installed providers support KeyPairGenerator.DSA");
+ }
+
+ for (int i = 0; i < providers.length; i++) {
+ KeyPairGenerator.getInstance("DSA", providers[i].getName());
+ }// end for
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find the provider");
+ }
+
+ // Test2: Test with empty provider name - should raise
+ // IllegalArgumentException
+ try {
+ KeyPairGenerator.getInstance("DSA", "");
+ fail("Should have thrown IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ } catch (Exception e) {
+ fail("Expected IllegalArgumentException, but got " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyPairGenerator#getProvider()
+ */
+ public void test_getProvider() {
+ try {
+ Provider p = KeyPairGenerator.getInstance("DSA").getProvider();
+ assertNotNull("provider is null", p);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ }
+ }
+
+ /**
+ * @tests java.security.KeyPairGenerator#initialize(int)
+ */
+ public void test_initializeI() {
+ try {
+ KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
+ keyPair.initialize(1024);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ }
+ }
+
+ /**
+ * @tests java.security.KeyPairGenerator#initialize(int,
+ * java.security.SecureRandom)
+ */
+ public void test_initializeILjava_security_SecureRandom() {
+ try {
+ KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
+ keyPair.initialize(1024, new SecureRandom());
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ }
+ }
+
+ /**
+ * @tests java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec)
+ */
+ public void test_initializeLjava_security_spec_AlgorithmParameterSpec() {
+ try {
+ KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
+ keyPair.initialize(new DSAParameterSpec(BigInteger.ONE,
+ BigInteger.ONE, BigInteger.ONE));
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ } catch (InvalidAlgorithmParameterException e) {
+ fail("caught a InvalidAlgorithmParameterException");
+ }
+ }
+
+ /**
+ * @tests java.security.KeyPairGenerator#initialize(java.security.spec.AlgorithmParameterSpec,
+ * java.security.SecureRandom)
+ */
+ public void test_initializeLjava_security_spec_AlgorithmParameterSpecLjava_security_SecureRandom() {
+ try {
+ KeyPairGenerator keyPair = KeyPairGenerator.getInstance("DSA");
+ keyPair.initialize(new DSAParameterSpec(BigInteger.ONE,
+ BigInteger.ONE, BigInteger.ONE), new SecureRandom());
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ } catch (InvalidAlgorithmParameterException e) {
+ fail("caught a InvalidAlgorithmParameterException");
+ }
+ }
+
+ 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/KeyStoreExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyStoreExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyStoreExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyStoreExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,56 @@
+/* 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.KeyStoreException;
+
+public class KeyStoreExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.KeyStoreException#KeyStoreException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.KeyStoreException()
+ KeyStoreException e = new KeyStoreException();
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals("java.security.KeyStoreException"));
+ }
+
+ /**
+ * @tests java.security.KeyStoreException#KeyStoreException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method java.security.KeyStoreException(java.lang.String)
+ KeyStoreException e = new KeyStoreException("test message");
+ assertTrue("Failed toString test for constructed instance", e
+ .toString().equals(
+ "java.security.KeyStoreException: 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/KeyStoreTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyStoreTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyStoreTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/KeyStoreTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,801 @@
+/* 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.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.KeyPair;
+import java.security.KeyPairGenerator;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+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.UnrecoverableKeyException;
+import java.security.cert.CertificateException;
+import java.security.cert.CertificateFactory;
+import java.security.cert.X509Certificate;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.HashSet;
+import java.util.Set;
+
+import tests.support.Support_TestProvider;
+import tests.support.resource.Support_Resources;
+
+public class KeyStoreTest extends junit.framework.TestCase {
+
+ final char[] pssWord = { 'a', 'b', 'c' };
+
+ // creating a certificate
+ 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());
+
+ String certificate3 = "-----BEGIN CERTIFICATE-----\n"
+ + "MIIDXDCCAsWgAwIBAgIBSjANBgkqhkiG9w0BAQUFADBWMQswCQYDVQQGEwJVUzEY\n"
+ + "MBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsT\n"
+ + "A1BLSTERMA8GA1UEAxMITWVkIENBLTEwHhcNOTgwODAyMTgwMjQwWhcNMDEwODAy\n"
+ + "MTgwMjQwWjB0MQswCQYDVQQGEwJVUzEYMBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50\n"
+ + "MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsTA1BLSTENMAsGA1UECxMEVVNBRjEgMB4G\n"
+ + "A1UEAxMXR3VtYnkuSm9zZXBoLjAwMDAwMDUwNDQwgZ8wDQYJKoZIhvcNAQEBBQAD\n"
+ + "gY0AMIGJAoGBALT/R7bPqs1c1YqXAg5HNpZLgW2HuAc7RCaP06cE4R44GBLw/fQc\n"
+ + "VRNLn5pgbTXsDnjiZVd8qEgYqjKFQka4/tNhaF7No2tBZB+oYL/eP0IWtP+h/W6D\n"
+ + "KR5+UvIIdgmx7k3t9jp2Q51JpHhhKEb9WN54trCO9Yu7PYU+LI85jEIBAgMBAAGj\n"
+ + "ggEaMIIBFjAWBgNVHSAEDzANMAsGCWCGSAFlAgELAzAfBgNVHSMEGDAWgBQzOhTo\n"
+ + "CWdhiGUkIOx5cELXppMe9jAdBgNVHQ4EFgQUkLBJl+ayKgzOp/wwBX9M1lSkCg4w\n"
+ + "DgYDVR0PAQH/BAQDAgbAMAwGA1UdEwEB/wQCMAAwgZ0GA1UdHwSBlTCBkjCBj6CB\n"
+ + "jKCBiYaBhmxkYXA6Ly9kcy0xLmNoYW1iLmRpc2EubWlsL2NuJTNkTWVkJTIwQ0El\n"
+ + "MmQxJTJjb3UlM2RQS0klMmNvdSUzZERvRCUyY28lM2RVLlMuJTIwR292ZXJubWVu\n"
+ + "dCUyY2MlM2RVUz9jZXJ0aWZpY2F0ZVJldm9jYXRpb25MaXN0JTNiYmluYXJ5MA0G\n"
+ + "CSqGSIb3DQEBBQUAA4GBAFjapuDHMvIdUeYRyEYdShBR1JZC20tJ3MQnyBQveddz\n"
+ + "LGFDGpIkRAQU7T/5/ne8lMexyxViC21xOlK9LdbJCbVyywvb9uEm/1je9wieQQtr\n"
+ + "kjykuB+WB6qTCIslAO/eUmgzfzIENvnH8O+fH7QTr2PdkFkiPIqBJYHvw7F3XDqy\n"
+ + "-----END CERTIFICATE-----\n";
+
+ ByteArrayInputStream certArray3 = new ByteArrayInputStream(certificate3
+ .getBytes());
+
+ private byte[] creatCertificate() {
+ ByteArrayOutputStream out = null;
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+ // alias 1
+ PublicKey pub = cert[0].getPublicKey();
+ keyTest.setCertificateEntry("alias1", cert[0]);
+
+ // alias 2
+ keyTest.setCertificateEntry("alias2", cert[0]);
+ keyTest.setKeyEntry("alias2", pub, pssWord, cert);
+
+ // alias 3
+ keyTest.setCertificateEntry("alias3", cert[1]);
+
+ out = new ByteArrayOutputStream();
+ keyTest.store(out, pssWord);
+ out.close();
+
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (KeyStoreException e) {
+ fail("problem setting keyEntry and CertificateEntry : " + e);
+ } catch (FileNotFoundException e) {
+ fail("file not found for writing : " + e);
+ } catch (IOException e) {
+ fail("An IO problem was found when writing the keystore: " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("The data integrity algorithm for the keystore cannot be found");
+ }
+
+ return out.toByteArray();
+ }
+
+ /**
+ * @tests java.security.KeyStore#aliases()
+ */
+ public void test_aliases() {
+ // Test for method java.util.Enumeration
+ // java.security.KeyStore.aliases()
+ // NOT COMPATIBLE WITH PCS#12
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+
+ // KeyStore keyTest =
+ // KeyStore.getInstance(KeyStore.getDefaultType());
+ // alias 1
+ keyTest.setCertificateEntry("alias1", cert[0]);
+
+ // alias 2
+ keyTest.setCertificateEntry("alias2", cert[0]);
+
+ // alias 3
+ keyTest.setCertificateEntry("alias3", cert[0]);
+
+ // obtaining the aliase
+ Enumeration aliase = keyTest.aliases();
+ Set alia = new HashSet();
+ int i = 0;
+ while (aliase.hasMoreElements()) {
+ alia.add(aliase.nextElement());
+ i++;
+ }
+ assertTrue("the alias names were returned wrong", i == 3
+ && alia.contains("alias1") && alia.contains("alias2")
+ && alia.contains("alias3"));
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (KeyStoreException e) {
+ fail("problem with setting keyEntry and CertificateEntry : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred");
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#containsAlias(java.lang.String)
+ */
+ public void test_containsAliasLjava_lang_String() {
+ // Test for method boolean
+ // java.security.KeyStore.containsAlias(java.lang.String)
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+
+ // alias 1
+ keyTest.setCertificateEntry("alias1", cert[0]);
+
+ // alias 2
+ keyTest.setCertificateEntry("alias2", cert[0]);
+
+ assertTrue("alias1 does not exist", keyTest.containsAlias("alias1"));
+ assertTrue("alias2 does not exist", keyTest.containsAlias("alias2"));
+ assertFalse("alias3 exists", keyTest.containsAlias("alias3"));
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (KeyStoreException e) {
+ fail("problem with setting keyEntry and CertificateEntry : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#getCertificate(java.lang.String)
+ */
+ public void test_getCertificateLjava_lang_String() {
+ // Test for method java.security.cert.Certificate
+ // java.security.KeyStore.getCertificate(java.lang.String)
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+
+ // alias 1
+ PublicKey pub = cert[0].getPublicKey();
+ keyTest.setCertificateEntry("alias1", cert[0]);
+
+ java.security.cert.Certificate certRes = keyTest
+ .getCertificate("alias1");
+ assertTrue(
+ "the public key of the certificate from getCertificate() "
+ + "did not equal the original certificate", certRes
+ .getPublicKey() == pub);
+
+ // alias 2
+ keyTest.setCertificateEntry("alias2", cert[0]);
+
+ // testing for a certificate chain
+ java.security.cert.Certificate cert2 = keyTest
+ .getCertificate("alias2");
+ assertTrue("the certificate for alias2 is supposed to exist",
+ cert2 != null && cert2.equals(cert[0]));
+
+ } catch (KeyStoreException e) {
+ fail("keyStore is not initialized : " + e);
+ } catch (CertificateException e) {
+ fail("the certificate is not generated correctly : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#getCertificateAlias(java.security.cert.Certificate)
+ */
+ public void test_getCertificateAliasLjava_security_cert_Certificate() {
+ // Test for method java.lang.String
+ // java.security.KeyStore.getCertificateAlias(java.security.cert.Certificate)
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+
+ // certificate entry
+ keyTest.setCertificateEntry("alias1", cert[1]);
+ String alias = keyTest.getCertificateAlias(cert[1]);
+ assertTrue("certificate entry - the alias returned for this "
+ + "certificate was wrong", alias.equals("alias1"));
+
+ // key entry
+ PublicKey pub = cert[0].getPublicKey();
+
+ // If next line throws a NPE check that a full math implementation
+ // is being used.
+ keyTest.setKeyEntry("alias2", pub, pssWord, cert);
+ alias = keyTest.getCertificateAlias(cert[0]);
+ assertTrue("key entry - the alias returned for this "
+ + "certificate was wrong", alias.equals("alias2"));
+
+ // testing case with a nonexistant certificate
+ X509Certificate cert2 = (X509Certificate) cf
+ .generateCertificate(certArray3);
+ String aliasNull = keyTest.getCertificateAlias(cert2);
+ assertNull("the alias returned for the nonexist certificate "
+ + "was NOT null", aliasNull);
+ } catch (KeyStoreException e) {
+ fail("unexpected keyStore exception : " + e);
+ } catch (CertificateException e) {
+ fail("the certificate is not generated correctly : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#getCertificateChain(java.lang.String)
+ */
+ public void test_getCertificateChainLjava_lang_String() {
+ // Test for method java.security.cert.Certificate []
+ // java.security.KeyStore.getCertificateChain(java.lang.String)
+ try {
+ // creatCertificate();
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+
+ // alias 1
+ PublicKey pub = cert[0].getPublicKey();
+ keyTest.setCertificateEntry("alias1", cert[0]);
+
+ // alias 2
+ keyTest.setCertificateEntry("alias2", cert[0]);
+ keyTest.setKeyEntry("alias2", pub, pssWord, cert);
+
+ java.security.cert.Certificate[] certRes = keyTest
+ .getCertificateChain("alias2");
+ assertTrue("there are more than two certificate returned "
+ + "from getCertificateChain", certRes.length == 2);
+ assertTrue("the certificates returned from getCertificateChain "
+ + "is not correct", cert[0].getPublicKey() == certRes[0]
+ .getPublicKey()
+ && cert[1].getPublicKey() == certRes[1].getPublicKey());
+ java.security.cert.Certificate[] certResNull = keyTest
+ .getCertificateChain("alias1");
+ assertTrue("the certificate chain returned from "
+ + "getCertificateChain is NOT null", certResNull == null);
+ } catch (KeyStoreException e) {
+ fail("keyStore is not initialized : " + e);
+ } catch (CertificateException e) {
+ fail("the certificate is not generated correctly : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#getInstance(java.lang.String)
+ */
+ public void test_getInstanceLjava_lang_String() {
+ // Test for method java.security.KeyStore
+ // java.security.KeyStore.getInstance(java.lang.String)
+ try {
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ assertTrue("the method getInstance did not obtain "
+ + "the correct type", keyTest.getType().equals(
+ KeyStore.getDefaultType()));
+ } catch (KeyStoreException e) {
+ fail("keyStore is not loaded : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#getInstance(java.lang.String,
+ * java.lang.String)
+ */
+ public void test_getInstanceLjava_lang_StringLjava_lang_String() {
+ // Test for method java.security.KeyStore
+ // java.security.KeyStore.getInstance(java.lang.String,
+ // java.lang.String)
+ try {
+ KeyStore keyTest = KeyStore.getInstance("PKCS#12/Netscape",
+ "TestProvider");
+ assertTrue("the method getInstance did not obtain the "
+ + "correct provider and type", keyTest.getProvider()
+ .getName().equals("TestProvider")
+ && keyTest.getType().equals("PKCS#12/Netscape"));
+ } catch (KeyStoreException e) {
+ fail("keyStore is not loaded : " + e);
+ } catch (NoSuchProviderException e) {
+ fail("no such provider : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#getKey(java.lang.String, char[])
+ */
+ public void test_getKeyLjava_lang_String$C() {
+
+ fail("Test hangs - requires a full math implementation ??");
+
+ // Test for method java.security.Key
+ // java.security.KeyStore.getKey(java.lang.String, char [])
+ try {
+ // creatCertificate();
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+ KeyPairGenerator keyPairGenerator = KeyPairGenerator
+ .getInstance("DSA");
+ SecureRandom secureRandom = new SecureRandom();
+ keyPairGenerator.initialize(1024, secureRandom);
+ KeyPair keyPair = keyPairGenerator.genKeyPair();
+ PrivateKey privateKey = keyPair.getPrivate();
+ keyTest.setKeyEntry("alias2", privateKey, pssWord, cert);
+ PrivateKey returnedKey = (PrivateKey) keyTest.getKey("alias2",
+ pssWord);
+ byte[] retB = returnedKey.getEncoded();
+ byte[] priB = privateKey.getEncoded();
+ boolean equality = Arrays.equals(retB, priB);
+ equality &= returnedKey.getAlgorithm().equals(
+ privateKey.getAlgorithm());
+ equality &= returnedKey.getFormat().equals(privateKey.getFormat());
+ assertTrue("the private key returned from getKey for a "
+ + "key entry did not equal the original key", equality);
+
+ try {
+ keyTest.getKey("alias2", "wrong".toCharArray());
+ fail("Should have thrown UnrecoverableKeyException");
+ } catch (UnrecoverableKeyException e) {
+ // expected
+ }
+
+ keyTest.setCertificateEntry("alias1", cert[1]);
+ assertNull("the private key returned from getKey for "
+ + "a certificate entry is not null", keyTest.getKey(
+ "alias1", pssWord));
+
+ } catch (KeyStoreException e) {
+ fail("type is not found : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("algorithm to recover the key cannot be found " + e);
+ } catch (UnrecoverableKeyException e) {
+ fail("the key can't be recovered : " + e);
+ } catch (CertificateException e) {
+ fail("the certificate is not generated correctly : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#getProvider()
+ */
+ public void test_getProvider() {
+ // Test for method java.security.Provider
+ // java.security.KeyStore.getProvider()
+ try {
+ KeyStore keyTest = KeyStore.getInstance("PKCS#12/Netscape",
+ "TestProvider");
+ Provider provKeyStore = keyTest.getProvider();
+ assertEquals("the provider should be TestProvider", "TestProvider",
+ provKeyStore.getName());
+ } catch (KeyStoreException e) {
+ fail("type is not found : " + e);
+ } catch (NoSuchProviderException e) {
+ fail("no such provider : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#getType()
+ */
+ public void test_getType() {
+ // Test for method java.lang.String java.security.KeyStore.getType()
+ try {
+ KeyStore keyTest = KeyStore.getInstance("PKCS#12/Netscape",
+ "TestProvider");
+ assertEquals(
+ "type should be PKCS#12/Netscape for provider TestProvider",
+ "PKCS#12/Netscape", keyTest.getType());
+ } catch (KeyStoreException e) {
+ fail("type is not found : " + e);
+ } catch (NoSuchProviderException e) {
+ fail("no such provider : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#isCertificateEntry(java.lang.String)
+ */
+ public void test_isCertificateEntryLjava_lang_String() {
+ // Test for method boolean
+ // java.security.KeyStore.isCertificateEntry(java.lang.String)
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+ // alias 1
+ PublicKey pub = cert[0].getPublicKey();
+ keyTest.setCertificateEntry("alias1", cert[0]);
+
+ // alias 2
+ keyTest.setCertificateEntry("alias2", cert[0]);
+ keyTest.setKeyEntry("alias2", pub, pssWord, cert);
+
+ assertTrue(
+ "isCertificateEntry method returns false for a certificate",
+ keyTest.isCertificateEntry("alias1") == true);
+ assertTrue(
+ "isCertificateEntry method returns true for noncertificate",
+ keyTest.isCertificateEntry("alias2") == false);
+ } catch (KeyStoreException e) {
+ fail("alias already exists for an untrusted certificate : " + e);
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#isKeyEntry(java.lang.String)
+ */
+ public void test_isKeyEntryLjava_lang_String() {
+ // Test for method boolean
+ // java.security.KeyStore.isKeyEntry(java.lang.String)
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+ // alias 1
+ PublicKey pub = cert[0].getPublicKey();
+ keyTest.setCertificateEntry("alias1", cert[0]);
+
+ // alias 2
+ keyTest.setCertificateEntry("alias2", cert[0]);
+ keyTest.setKeyEntry("alias2", pub, pssWord, cert);
+
+ assertTrue("isKeyEntry method returns false for a certificate",
+ keyTest.isKeyEntry("alias2") == true);
+ assertTrue("isKeyEntry method returns true for noncertificate",
+ keyTest.isKeyEntry("alias1") == false);
+ } catch (KeyStoreException e) {
+ fail("alias already exists for an untrusted certificate : " + e);
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#load(java.io.InputStream, char[])
+ */
+ public void test_loadLjava_io_InputStream$C() {
+ // Test for method void java.security.KeyStore.load(java.io.InputStream,
+ // char [])
+ try {
+ byte[] keyStore = creatCertificate();
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ InputStream in = new ByteArrayInputStream(keyStore);
+ keyTest.load(in, pssWord);
+ in.close();
+ assertTrue("alias1 is not a certificate", keyTest
+ .isCertificateEntry("alias1") == true);
+ assertTrue("alias2 is not a keyEntry",
+ keyTest.isKeyEntry("alias2") == true);
+ assertTrue("alias3 is not a certificate", keyTest
+ .isCertificateEntry("alias3") == true);
+
+ // test with null password
+ keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ in = new ByteArrayInputStream(keyStore);
+ keyTest.load(in, null);
+ in.close();
+ assertTrue("alias1 is not a certificate", keyTest
+ .isCertificateEntry("alias1") == true);
+ assertTrue("alias2 is not a keyEntry",
+ keyTest.isKeyEntry("alias2") == true);
+ assertTrue("alias3 is not a certificate", keyTest
+ .isCertificateEntry("alias3") == true);
+
+ keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ InputStream v1in = Support_Resources.getStream("hyts_ks.bks");
+ char[] pass = "abcdef".toCharArray();
+ keyTest.load(v1in, pass);
+ v1in.close();
+ keyTest.getKey("mykey", pass);
+ } catch (UnrecoverableKeyException e) {
+ fail("Caught an UnrecoverableKeyException : " + e);
+ } catch (KeyStoreException e) {
+ fail("alias already exists for an untrusted certificate : " + e);
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (IOException e) {
+ e.printStackTrace();
+ fail("An IO problem was found when reading the keystore : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("The data integrity algorithm for the keystore "
+ + "cannot be found : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#setCertificateEntry(java.lang.String,
+ * java.security.cert.Certificate)
+ */
+ public void test_setCertificateEntryLjava_lang_StringLjava_security_cert_Certificate() {
+ // Test for method void
+ // java.security.KeyStore.setCertificateEntry(java.lang.String,
+ // java.security.cert.Certificate)
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert = (X509Certificate) cf
+ .generateCertificate(certArray);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+
+ PublicKey pub = cert.getPublicKey();
+ keyTest.setCertificateEntry("alias1", cert);
+ assertTrue(
+ "the entry specified by the alias alias1 is not a certificate",
+ keyTest.isCertificateEntry("alias1") == true);
+ java.security.cert.Certificate resultCert = keyTest
+ .getCertificate("alias1");
+ assertTrue(
+ "the public key of the certificate from getCertificate() did not equal the original certificate",
+ resultCert.getPublicKey() == pub);
+ } catch (KeyStoreException e) {
+ fail("alias already exists for an untrusted certificate : " + e);
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+
+ }
+
+ /**
+ * @tests java.security.KeyStore#setKeyEntry(java.lang.String, byte[],
+ * java.security.cert.Certificate[])
+ */
+ public void test_setKeyEntryLjava_lang_String$B$Ljava_security_cert_Certificate() {
+
+ fail("Test hangs - requires a full math implementation ??");
+
+ // Test for method void
+ // java.security.KeyStore.setKeyEntry(java.lang.String, byte [],
+ // java.security.cert.Certificate [])
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+ // generator
+ KeyPairGenerator keyPairGenerator = KeyPairGenerator
+ .getInstance("DSA");
+ SecureRandom secureRandom = new SecureRandom();
+ keyPairGenerator.initialize(1024, secureRandom);
+ KeyPair keyPair = keyPairGenerator.genKeyPair();
+ // set the same alias as keyEntry
+ keyTest.setKeyEntry("alias2", keyPair.getPrivate().getEncoded(),
+ cert);
+ assertTrue(
+ "the entry specified by the alias alias2 is not a keyEntry",
+ keyTest.isKeyEntry("alias2"));
+ } catch (KeyStoreException e) {
+ fail("Setting keyEntry for the alias failed : " + e);
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#setKeyEntry(java.lang.String,
+ * java.security.Key, char[], java.security.cert.Certificate[])
+ */
+ public void test_setKeyEntryLjava_lang_StringLjava_security_Key$C$Ljava_security_cert_Certificate() {
+
+ fail("Test hangs - requires a full math implementation ??");
+
+ // Test for method void
+ // java.security.KeyStore.setKeyEntry(java.lang.String,
+ // java.security.Key, char [], java.security.cert.Certificate [])
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+ // generator
+ KeyPairGenerator keyPairGenerator = KeyPairGenerator
+ .getInstance("DSA");
+ SecureRandom secureRandom = new SecureRandom();
+ keyPairGenerator.initialize(1024, secureRandom);
+ KeyPair keyPair = keyPairGenerator.genKeyPair();
+ PrivateKey privateKey = keyPair.getPrivate();
+ keyTest.setKeyEntry("alias3", privateKey, pssWord, cert);
+ assertTrue(
+ "the entry specified by the alias alias3 is not a keyEntry",
+ keyTest.isKeyEntry("alias3"));
+ } catch (KeyStoreException e) {
+ fail("Setting keyEntry for the alias failed : " + e);
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.KeyStore#size()
+ */
+ public void test_size() {
+ // Test for method int java.security.KeyStore.size()
+ try {
+ CertificateFactory cf = CertificateFactory.getInstance("X.509");
+ X509Certificate cert[] = new X509Certificate[2];
+ cert[0] = (X509Certificate) cf.generateCertificate(certArray);
+ cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
+ KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
+ keyTest.load(null, null);
+ // alias 1
+ PublicKey pub = cert[0].getPublicKey();
+ keyTest.setCertificateEntry("alias1", cert[0]);
+
+ // alias 2
+ keyTest.setCertificateEntry("alias2", cert[0]);
+ keyTest.setKeyEntry("alias2", pub, pssWord, cert);
+
+ // alias 3
+ keyTest.setCertificateEntry("alias3", cert[1]);
+
+ assertTrue("the size of the keyStore is not 3", keyTest.size() == 3);
+ } catch (KeyStoreException e) {
+ fail("alias already exists for an untrusted certificate : " + e);
+ } catch (CertificateException e) {
+ fail("creating a certificate failed : " + e);
+ } catch (IOException e) {
+ fail("IOException occurred : " + e);
+ } catch (NoSuchAlgorithmException e) {
+ fail("NoSuchAlgorithmException occurred : " + e);
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ Security.addProvider(new Support_TestProvider());
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ Security.removeProvider("TestProvider");
+ }
+}
\ No newline at end of file
Added: incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/MessageDigestTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/MessageDigestTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/MessageDigestTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/MessageDigestTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,447 @@
+/* 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.io.DataOutputStream;
+import java.io.IOException;
+import java.security.DigestException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+import java.security.Provider;
+import java.security.Security;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.Vector;
+
+public class MessageDigestTest extends junit.framework.TestCase {
+
+ private static final String MESSAGEDIGEST_ID = "MessageDigest.";
+
+ private String[] digestAlgs = null;
+
+ private String providerName = null;
+
+ private static final byte[] AR1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
+
+ private static final byte[] AR2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
+
+ private static final String MESSAGE = "abc";
+
+ private static final byte[] MESSAGE_DIGEST = { -87, -103, 62, 54, 71, 6,
+ -127, 106, -70, 62, 37, 113, 120, 80, -62, 108, -100, -48, -40,
+ -99, };
+
+ private static final byte[] MESSAGE_DIGEST_63_As = { 3, -16, -97, 91, 21,
+ -118, 122, -116, -38, -39, 32, -67, -36, 41, -72, 28, 24, -91, 81,
+ -11, };
+
+ private static final byte[] MESSAGE_DIGEST_64_As = { 0, -104, -70, -126,
+ 75, 92, 22, 66, 123, -41, -95, 18, 42, 90, 68, 42, 37, -20, 100,
+ 77, };
+
+ private static final byte[] MESSAGE_DIGEST_65_As = { 17, 101, 83, 38, -57,
+ 8, -41, 3, 25, -66, 38, 16, -24, -91, 125, -102, 91, -107, -99, 59, };
+
+ /**
+ * @tests java.security.MessageDigest#clone()
+ */
+ public void test_clone() {
+ for (int i = 0; i < digestAlgs.length; i++) {
+ try {
+ MessageDigest d1 = MessageDigest.getInstance(digestAlgs[i],
+ providerName);
+ for (byte b = 0; b < 84; b++) {
+ d1.update(b);
+ }
+
+ MessageDigest d2 = (MessageDigest) d1.clone();
+ d1.update((byte) 1);
+ d2.update((byte) 1);
+
+ assertTrue("cloned hash differs from original for algorithm "
+ + digestAlgs[i], MessageDigest.isEqual(d1.digest(), d2
+ .digest()));
+ } catch (CloneNotSupportedException e) {
+ // Expected - a Signature may not be cloneable
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + digestAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ }
+ }
+ }
+
+ /**
+ * Convenience method to print out a byte array
+ *
+ * @param arr
+ * an array of bytes
+ */
+ private void printDigestArray(byte[] arr) {
+ System.out.print('{');
+ for (int i = 0; i < arr.length; i++) {
+ System.out.print(arr[i]);
+ System.out.print(',');
+ }
+ System.out.print('}');
+ }
+
+ private static final byte[] SHA_DATA_2 = { 70, -54, 124, 120, -29, 57, 56,
+ 119, -108, -54, -97, -76, -97, -50, -63, -73, 2, 85, -53, -79, };
+
+ private void testSerializationSHA_DATA_2(MessageDigest sha) {
+ try {
+ sha.reset();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ DataOutputStream output = new DataOutputStream(out);
+ // -----------------------------------------------------------------------
+
+ // Made up data
+ output
+ .writeUTF("tests.api.java.security.MessageDigestTest$InitializerFieldsTest3");
+ output.writeInt(0); // class modifiers
+ output.writeUTF("java.io.Serializable"); // interfaces
+
+ // Fields
+ output.writeUTF("sub_toBeNotSerialized"); // name
+ output.writeInt(9); // modifiers
+ output.writeUTF("Ljava/lang/String;"); // signature
+
+ output.writeUTF("sub_toBeNotSerialized2"); // name
+ output.writeInt(9); // modifiers
+ output.writeUTF("Ljava/lang/String;"); // signature
+
+ output.writeUTF("sub_toBeSerialized"); // name
+ output.writeInt(1); // modifiers
+ output.writeUTF("Ljava/lang/String;"); // signature
+
+ output.writeUTF("sub_toBeSerialized3"); // name
+ output.writeInt(1); // modifiers
+ output.writeUTF("Ljava/lang/String;"); // signature
+
+ output.writeUTF("sub_toBeSerialized4"); // name
+ output.writeInt(1); // modifiers
+ output.writeUTF("Ljava/lang/String;"); // signature
+
+ output.writeUTF("sub_toBeSerialized5"); // name
+ output.writeInt(1); // modifiers
+ output.writeUTF("Ljava/lang/String;"); // signature
+
+ // clinit
+ output.writeUTF("<clinit>"); // name
+ output.writeInt(8); // modifiers
+ output.writeUTF("()V"); // signature
+
+ // constructors
+ output.writeUTF("<init>"); // name
+ output.writeInt(0); // modifiers
+ output.writeUTF("()V"); // signature
+
+ // methods
+ output.writeUTF("equals"); // name
+ output.writeInt(1); // modifiers
+ output.writeUTF("(Ljava.lang.Object;)Z"); // signature
+
+ // -----------------------------------------------------------------------
+
+ output.flush();
+
+ byte[] data = out.toByteArray();
+ byte[] hash = sha.digest(data);
+ assertTrue("SHA_DATA_2 NOT ok", Arrays.equals(hash, SHA_DATA_2));
+ } catch (IOException e) {
+ fail("SHA_DATA_2 NOT ok");
+ }
+ }
+
+ private static final byte[] SHA_DATA_1 = { 90, 36, 111, 106, -32, 38, 4,
+ 126, 21, -51, 107, 45, -64, -68, -109, 112, -31, -46, 34, 115, };
+
+ private void testSerializationSHA_DATA_1(MessageDigest sha) {
+ try {
+ sha.reset();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ DataOutputStream output = new DataOutputStream(out);
+ // -----------------------------------------------------------------------
+
+ // Made up data
+ output
+ .writeUTF("tests.api.java.security.MessageDigestTest$OptionalDataNotRead");
+ // name
+ output.writeInt(0); // class modifiers
+ output.writeUTF("java.io.Serializable"); // interfaces
+
+ // Fields
+ output.writeUTF("class$0"); // name
+ output.writeInt(8); // modifiers
+ output.writeUTF("Ljava/lang/Class;"); // signature
+
+ output.writeUTF("field1"); // name
+ output.writeInt(2); // modifiers
+ output.writeUTF("I"); // signature
+
+ output.writeUTF("field2"); // name
+ output.writeInt(2); // modifiers
+ output.writeUTF("I"); // signature
+
+ // clinit
+ output.writeUTF("<clinit>"); // name
+ output.writeInt(8); // modifiers
+ output.writeUTF("()V"); // signature
+
+ // constructors
+ output.writeUTF("<init>"); // name
+ output.writeInt(1); // modifiers
+ output.writeUTF("()V"); // signature
+ // -----------------------------------------------------------------------
+
+ output.flush();
+ byte[] data = out.toByteArray();
+ byte[] hash = sha.digest(data);
+ assertTrue("SHA_DATA_1 NOT ok", Arrays.equals(hash, SHA_DATA_1));
+ } catch (IOException e) {
+ fail("SHA_DATA_1 NOT ok");
+ }
+ }
+
+ /**
+ * @tests java.security.MessageDigest#digest()
+ */
+ public void test_digest() {
+ MessageDigest sha = null;
+ try {
+ sha = MessageDigest.getInstance("SHA");
+ assertNotNull(sha);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ }
+ sha.update(MESSAGE.getBytes());
+ byte[] digest = sha.digest();
+ assertTrue("bug in SHA", MessageDigest.isEqual(digest, MESSAGE_DIGEST));
+
+ sha.reset();
+ for (int i = 0; i < 63; i++) {
+ // just under buffer capacity
+ sha.update((byte) 'a');
+ }
+ digest = sha.digest();
+ assertTrue("bug in SHA", MessageDigest.isEqual(digest,
+ MESSAGE_DIGEST_63_As));
+
+ sha.reset();
+ for (int i = 0; i < 64; i++) {
+ // exact SHA buffer capacity
+ sha.update((byte) 'a');
+ }
+ digest = sha.digest();
+ assertTrue("bug in SHA", MessageDigest.isEqual(digest,
+ MESSAGE_DIGEST_64_As));
+
+ sha.reset();
+ for (int i = 0; i < 65; i++) {
+ // just above SHA buffer capacity
+ sha.update((byte) 'a');
+ }
+ digest = sha.digest();
+ assertTrue("bug in SHA", MessageDigest.isEqual(digest,
+ MESSAGE_DIGEST_65_As));
+
+ testSerializationSHA_DATA_1(sha);
+ testSerializationSHA_DATA_2(sha);
+ }
+
+ /**
+ * @tests java.security.MessageDigest#digest(byte[])
+ */
+ public void test_digest$B() {
+ for (int i = 0; i < digestAlgs.length; i++) {
+ try {
+ MessageDigest digest = MessageDigest.getInstance(digestAlgs[i],
+ providerName);
+ assertNotNull(digest);
+ digest.digest(AR1);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + digestAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.MessageDigest#digest(byte[], int, int)
+ */
+ public void test_digest$BII() {
+ for (int i = 0; i < digestAlgs.length; i++) {
+ try {
+ MessageDigest digest = MessageDigest.getInstance(digestAlgs[i],
+ providerName);
+ assertNotNull(digest);
+ int len = digest.getDigestLength();
+ byte[] digestBytes = new byte[len];
+ digest.digest(digestBytes, 0, digestBytes.length);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + digestAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ } catch (DigestException e) {
+ fail("digest caused exception for algorithm " + digestAlgs[i]
+ + " : " + e);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.MessageDigest#getAlgorithm()
+ */
+ public void test_getAlgorithm() {
+ for (int i = 0; i < digestAlgs.length; i++) {
+ try {
+ String alg = MessageDigest.getInstance(digestAlgs[i],
+ providerName).getAlgorithm();
+ assertTrue("getAlgorithm ok", alg.equals(digestAlgs[i]));
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + digestAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.MessageDigest#getDigestLength()
+ */
+ public void test_getDigestLength() {
+ for (int i = 0; i < digestAlgs.length; i++) {
+ try {
+ int len = MessageDigest
+ .getInstance(digestAlgs[i], providerName)
+ .getDigestLength();
+ assertTrue("length not ok", len > 0);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + digestAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ }
+ }// end for
+ }
+
+ /**
+ * @tests java.security.MessageDigest#getInstance(java.lang.String)
+ */
+ public void test_getInstanceLjava_lang_String() {
+ for (int i = 0; i < digestAlgs.length; i++) {
+ try {
+ MessageDigest.getInstance(digestAlgs[i]);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + digestAlgs[i]);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.MessageDigest#getInstance(java.lang.String,
+ * java.lang.String)
+ */
+ public void test_getInstanceLjava_lang_StringLjava_lang_String() {
+ for (int i = 0; i < digestAlgs.length; i++) {
+ try {
+ MessageDigest.getInstance(digestAlgs[i], providerName);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + digestAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.MessageDigest#getProvider()
+ */
+ public void test_getProvider() {
+ for (int i = 0; i < digestAlgs.length; i++) {
+ try {
+ Provider p = MessageDigest.getInstance(digestAlgs[i],
+ providerName).getProvider();
+ assertNotNull("provider is null", p);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm " + digestAlgs[i]);
+ } catch (NoSuchProviderException e) {
+ fail("getInstance did not find provider " + providerName);
+ }
+ }
+ }
+
+ /**
+ * @tests java.security.MessageDigest#isEqual(byte[], byte[])
+ */
+ public void test_isEqual$B$B() {
+ assertTrue("isEqual is not correct", MessageDigest.isEqual(AR1, AR2));
+ }
+
+ /**
+ * @tests java.security.MessageDigest#toString()
+ */
+ public void test_toString() {
+ try {
+ String str = MessageDigest.getInstance("SHA").toString();
+ assertNotNull("toString is null", str);
+ } catch (NoSuchAlgorithmException e) {
+ fail("getInstance did not find algorithm");
+ }
+ }
+
+ protected void setUp() {
+ if (digestAlgs == null) {
+ Provider[] providers = Security.getProviders("MessageDigest.SHA");
+ if (providers == null) {
+ fail("No providers available for test");
+ }
+
+ // Arbitrarily select the first available provider
+ providerName = providers[0].getName();
+ digestAlgs = getDigestAlgorithms(providerName);
+ if (digestAlgs == null || digestAlgs.length == 0) {
+ fail("No digest algorithms were found");
+ }
+ }
+ }
+
+ /*
+ * Returns the digest algorithms that the given provider supports.
+ */
+ private String[] getDigestAlgorithms(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(MESSAGEDIGEST_ID)) {
+ algs.addElement(algorithm.substring(MESSAGEDIGEST_ID.length()));
+ }
+ }// end while
+
+ return (String[]) algs.toArray(new String[algs.size()]);
+ }
+
+ 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/NoSuchAlgorithmExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/NoSuchAlgorithmExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/NoSuchAlgorithmExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/NoSuchAlgorithmExceptionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,64 @@
+/* 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.NoSuchAlgorithmException;
+
+public class NoSuchAlgorithmExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.NoSuchAlgorithmException#NoSuchAlgorithmException()
+ */
+ public void test_Constructor() {
+ try {
+ throw new NoSuchAlgorithmException();
+ } catch (NoSuchAlgorithmException e) {
+ assertNull("Message should be null", e.getMessage());
+ assertEquals("Unexpected toString value",
+ "java.security.NoSuchAlgorithmException", e.toString());
+ }
+ }
+
+ /**
+ * @tests java.security.NoSuchAlgorithmException#NoSuchAlgorithmException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method
+ // java.security.NoSuchAlgorithmException(java.lang.String)
+ try {
+ throw new NoSuchAlgorithmException("Test string");
+ } catch (NoSuchAlgorithmException e) {
+ assertEquals("Wrong message", "Test string", e.getMessage());
+ assertEquals("Unexpected toString value",
+ "java.security.NoSuchAlgorithmException: Test string", 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/NoSuchProviderExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/NoSuchProviderExceptionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/NoSuchProviderExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/NoSuchProviderExceptionTest.java Sun Apr 9 22:38:31 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.
+ */
+
+package tests.api.java.security;
+
+import java.security.NoSuchProviderException;
+
+public class NoSuchProviderExceptionTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.security.NoSuchProviderException#NoSuchProviderException()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.NoSuchProviderException()
+ try {
+ throw new NoSuchProviderException();
+ } catch (NoSuchProviderException e) {
+ assertNull("Message should be null", e.getMessage());
+ assertEquals("Unexpected toString value",
+ "java.security.NoSuchProviderException", e.toString());
+ }
+ }
+
+ /**
+ * @tests java.security.NoSuchProviderException#NoSuchProviderException(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // Test for method
+ // java.security.NoSuchProviderException(java.lang.String)
+ try {
+ throw new NoSuchProviderException("Test string");
+ } catch (NoSuchProviderException e) {
+ assertEquals("Wrong message", "Test string", e.getMessage());
+ assertEquals("Unexpected toString value",
+ "java.security.NoSuchProviderException: Test string", 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/PermissionCollectionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionCollectionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionCollectionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionCollectionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,253 @@
+/* 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.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.security.PermissionCollection;
+import java.security.SecurityPermission;
+import java.util.NoSuchElementException;
+import java.util.StringTokenizer;
+
+import tests.support.Support_Exec;
+import tests.support.Support_GetLocal;
+
+public class PermissionCollectionTest extends junit.framework.TestCase {
+
+ // The below test is known to fail. Haven't got to the bottom of
+ // it yet but here is what has been determined :-
+ //
+ // * the Support_PermissionCollection application that is forked off
+ // near the end of this test needs to verify a signed jar (signedBKS.jar).
+ // This means that com.ibm.oti.util.JarUtils.verifySignature() ends up
+ // getting called. But at present that exists as just a lightweight/stub
+ // implementation which simply returns NULL. That behaviour causes a
+ // security exception inside java.util.jar.JarVerifier.
+ //
+ // * the above problem was fixed by rebuilding Harmony with the STUB
+ // IMPLEMENTATION of com.ibm.oti.util.JarUtils.verifySignature() replaced
+ // with one that delegates to
+ // org.apache.harmony.security.utils.JarUtils.verifySignature().
+ //
+ // * unfortunately, a NPE is raised in line 103 of Harmony's JarUtils class.
+ //
+ // * the cause of that NPE has still not been determined. Could it be
+ // related to Harmony's current stub implementation of BigInteger ?
+ /**
+ * @tests java.security.PermissionCollection#implies(java.security.Permission)
+ */
+ public void test_impliesLjava_security_Permission() {
+
+ // Look for the tests classpath
+ URL classURL = this.getClass().getProtectionDomain().getCodeSource()
+ .getLocation();
+ if (classURL == null) {
+ fail("Could not get this class' location");
+ }
+
+ File policyFile = null;
+ try {
+ policyFile = Support_GetLocal.createTempFile(".policy");
+ } catch (IOException e) {
+ fail("Unexpected IOException while creating policy file : " + e);
+ }
+
+ // Create the policy file (and save the existing one if any)
+ try {
+ FileOutputStream fileOut = new FileOutputStream(policyFile);
+ String linebreak = System.getProperty("line.separator");
+ String towrite = "grant codeBase \""
+ + classURL.toExternalForm()
+ + "tests/resources/PermissionCollection/signedBKS.jar"
+ + "\" signedBy \"eleanor\" {"
+ + linebreak
+ + "permission java.io.FilePermission \"test1.txt\", \"write\";"
+ + linebreak
+ + "permission mypackage.MyPermission \"essai\", signedBy \"eleanor,dylan\";"
+ + linebreak
+ + "};"
+ + linebreak
+ + "grant codeBase \""
+ + classURL.toExternalForm()
+ + "tests/resources/PermissionCollection/signedBKS.jar"
+ + "\" signedBy \"eleanor\" {"
+ + linebreak
+ + "permission java.io.FilePermission \"test2.txt\", \"write\";"
+ + linebreak + "};" + linebreak + "grant codeBase \"";
+ towrite += classURL.toExternalForm();
+ towrite += "\" {" + linebreak
+ + "permission java.security.AllPermission;" + linebreak
+ + "};" + linebreak + "keystore \""
+ + classURL.toExternalForm()
+ + "tests/resources/PermissionCollection/keystore.bks"
+ + "\",\"BKS\";";
+ fileOut.write(towrite.getBytes());
+ fileOut.flush();
+ fileOut.close();
+ } catch (Exception e) {
+ fail("Exception while creating policy file : " + e);
+ }
+
+ // Copy mypermissionBKS.jar to the user directory so that it can be put
+ // in
+ // the classpath
+ File jarFile = null;
+ try {
+ URL jarURL = new URL(
+ classURL.toExternalForm()
+ + "tests/resources/PermissionCollection/mypermissionBKS.jar");
+ jarFile = Support_GetLocal.createTempFile(".jar");
+ FileOutputStream fout = new FileOutputStream(jarFile);
+ InputStream jis = jarURL.openStream();
+ int c = jis.read();
+ while (c != -1) {
+ fout.write(c);
+ c = jis.read();
+ }
+ fout.flush();
+ fout.close();
+ jis.close();
+ } catch (MalformedURLException e) {
+ fail("Unexpected MalformedURLException while creating jar file :"
+ + e);
+ } catch (FileNotFoundException e) {
+ fail("Unexpected FileNotFoundException while creating jar file :"
+ + e);
+ } catch (IOException e) {
+ fail("Unexpected IOException while creating jar file : " + e);
+ }
+
+ String classPath = new File(classURL.getFile()).getPath();
+
+ // Execute Support_PermissionCollection in another VM
+ String[] classPathArray = new String[2];
+ classPathArray[0] = classPath;
+ classPathArray[1] = jarFile.getPath();
+ String[] args = new String[3];
+ try {
+ args[0] = "-Djava.security.policy=" + policyFile.toURL();
+ args[1] = "tests.support.Support_PermissionCollection";
+ args[2] = classURL.toExternalForm()
+ + "tests/resources/PermissionCollection/signedBKS.jar";
+ } catch (MalformedURLException e) {
+ fail("Unexpected MalformedURLException while policy file to url : "
+ + e);
+ }
+
+ try {
+ String result = Support_Exec.execJava(args, classPathArray, true);
+ // Delete the Jar file copied in the user directory
+ if (!jarFile.delete()) {
+ throw new IOException("Could not delete temporary jar file : "
+ + jarFile.getPath());
+ }
+
+ // Delete the temporary policy file
+ if (!policyFile.delete()) {
+ throw new IOException(
+ "Could not delete temporary policy file : "
+ + policyFile.getPath());
+ }
+
+ StringTokenizer resultTokenizer = new StringTokenizer(result, ",");
+
+ // Check the test result from the new VM process
+ assertTrue("Permission should be granted", resultTokenizer
+ .nextToken().equals("true"));
+ assertTrue("signed Permission should be granted", resultTokenizer
+ .nextToken().equals("true"));
+ assertTrue("Permission should not be granted", resultTokenizer
+ .nextToken().equals("false"));
+ } catch (IOException e) {
+ fail("IOException during test : " + e);
+ } catch (InterruptedException e) {
+ fail("InterruptedException during test : " + e);
+ } catch (NoSuchElementException e) {
+ fail("NoSuchElementException during test : " + e);
+ } catch (Exception e) {
+ fail("Exception during test : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.PermissionCollection#PermissionCollection()
+ */
+ public void test_Constructor() {
+ // test java.security.permissionCollection.PermissionCollection()
+ try {
+ SecurityPermission permi = new SecurityPermission(
+ "testing permissionCollection-isReadOnly");
+ PermissionCollection permCollect = permi.newPermissionCollection();
+ assertNotNull(
+ "creat permissionCollection constructor returned a null",
+ permCollect);
+ } catch (Exception e) {
+ fail("creating of permissionCollection constructor failed : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.PermissionCollection#isReadOnly()
+ */
+ public void test_isReadOnly() {
+ // test java.security.permissionCollection.isReadOnly()
+ SecurityPermission permi = new SecurityPermission(
+ "testing permissionCollection-isREadOnly");
+ PermissionCollection permCollect = permi.newPermissionCollection();
+ assertTrue("readOnly has not been set, but isReadOnly returned true",
+ !permCollect.isReadOnly());
+ permCollect.setReadOnly();
+ assertTrue("readOnly is set, but isReadonly returned false",
+ permCollect.isReadOnly());
+ }
+
+ /**
+ * @tests java.security.PermissionCollection#setReadOnly()
+ */
+ public void test_setReadOnly() {
+ // test java.security.permissionCollection.setReadOnly()
+ SecurityPermission permi = new SecurityPermission(
+ "testing permissionCollection-setReadOnly");
+ PermissionCollection permCollect = permi.newPermissionCollection();
+ assertTrue("readOnly has not been set, but isReadOnly returned true",
+ !permCollect.isReadOnly());
+ permCollect.setReadOnly();
+ assertTrue("readOnly is set, but isReadonly returned false",
+ permCollect.isReadOnly());
+ }
+
+ /**
+ * @tests java.security.PermissionCollection#toString()
+ */
+ public void test_toString() {
+ // test java.security.permissionCollection.toString()
+ SecurityPermission permi = new SecurityPermission(
+ "testing permissionCollection-isREadOnly");
+ assertNotNull("toString should have returned a string of elements",
+ permi.newPermissionCollection().toString());
+ }
+
+ 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/PermissionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,116 @@
+/* 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.Permission;
+import java.security.PermissionCollection;
+import java.security.SecurityPermission;
+
+public class PermissionTest extends junit.framework.TestCase {
+ static class ConcretePermission extends Permission {
+ public ConcretePermission() {
+ super("noname");
+ }
+
+ public boolean equals(Object obj) {
+ return true;
+ };
+
+ public String getActions() {
+ return "none";
+ };
+
+ public int hashCode() {
+ return 1;
+ };
+
+ public boolean implies(Permission p) {
+ return true;
+ };
+ }
+
+ /**
+ * @tests java.security.Permission#Permission(java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_String() {
+ // test method java.security.permission.Permission(string)
+ SecurityPermission permi = new SecurityPermission(
+ "Testing the permission abstract class");
+ String name = permi.getName();
+ assertEquals("Permission Constructor failed",
+ "Testing the permission abstract class", name);
+ }
+
+ /**
+ * @tests java.security.Permission#checkGuard(java.lang.Object)
+ */
+ public void test_checkGuardLjava_lang_Object() {
+ // test method java.security.permission.checkGuard(object)
+ SecurityPermission permi = new SecurityPermission(
+ "Testing the permission abstract class");
+ String name = permi.getName();
+ try {
+ permi.checkGuard(name);
+ } catch (SecurityException e) {
+ fail("security not granted when it is suppose to be : " + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Permission#getName()
+ */
+ public void test_getName() {
+ // test method java.security.permission.getName()
+ SecurityPermission permi = new SecurityPermission("testing getName()");
+ String name = permi.getName();
+ assertEquals("getName failed to obtain the correct name",
+ "testing getName()", name);
+
+ SecurityPermission permi2 = new SecurityPermission("93048Helloworld");
+ assertEquals("getName failed to obtain correct name",
+ "93048Helloworld", permi2.getName());
+ }
+
+ /**
+ * @tests java.security.Permission#newPermissionCollection()
+ */
+ public void test_newPermissionCollection() {
+ // test method java.security.permission.newPermissionCollection
+ Permission permi = new ConcretePermission();
+ PermissionCollection permiCollect = permi.newPermissionCollection();
+ assertNull("newPermissionCollector of the abstract class "
+ + "permission did not return a null instance "
+ + "of permissionCollection", permiCollect);
+ }
+
+ /**
+ * @tests java.security.Permission#toString()
+ */
+ public void test_toString() {
+ // test method java.security.permission.toString
+ // test for permission with no action
+ SecurityPermission permi = new SecurityPermission("testing toString");
+ String toString = permi.toString();
+ assertNotNull("toString should have returned a string of elements",
+ toString);
+ }
+
+ 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/PermissionsTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionsTest.java?rev=392891&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionsTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/security/src/test/java/common/tests/api/java/security/PermissionsTest.java Sun Apr 9 22:38:31 2006
@@ -0,0 +1,169 @@
+/* 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.File;
+import java.io.FilePermission;
+import java.security.Permissions;
+import java.util.Enumeration;
+
+public class PermissionsTest extends junit.framework.TestCase {
+ FilePermission readAllFiles = new FilePermission("<<ALL FILES>>", "read");
+
+ FilePermission alsoReadAllFiles = new FilePermission("<<ALL FILES>>",
+ "read");
+
+ FilePermission allInCurrent = new FilePermission("*",
+ "read, write, execute,delete");
+
+ FilePermission readInCurrent = new FilePermission("*", "read");
+
+ FilePermission readInFile = new FilePermission("aFile.file", "read");
+
+ /**
+ * @tests java.security.Permissions#Permissions()
+ */
+ public void test_Constructor() {
+ // Test for method java.security.Permissions()
+ try {
+ new Permissions();
+ } catch (Exception e) {
+ fail("creating an instance of permissions constructor failed : "
+ + e);
+ }
+ }
+
+ /**
+ * @tests java.security.Permissions#add(java.security.Permission)
+ */
+ public void test_addLjava_security_Permission() {
+ // Test for method void
+ // java.security.Permissions.add(java.security.Permission)
+ char s = File.separatorChar;
+ FilePermission perm[] = new FilePermission[7];
+ perm[0] = readAllFiles;
+ perm[1] = allInCurrent;
+ perm[2] = new FilePermission(s + "tmp" + s + "test" + s + "*",
+ "read,write");
+ perm[3] = new FilePermission(s + "tmp" + s + "test" + s
+ + "collection.file", "read");
+ perm[4] = alsoReadAllFiles;
+ perm[5] = readInFile;
+ perm[6] = new FilePermission("hello.file", "write");
+ Permissions perms = new Permissions();
+ try {
+ for (int i = 0; i < perm.length; i++) {
+ perms.add(perm[i]);
+ }
+ } catch (IllegalStateException e) {
+ fail("add method failed with unexpected IllegalStateException : "
+ + e);
+ }
+
+ Enumeration e = perms.elements();
+ FilePermission perm2[] = new FilePermission[10];
+ int i = 0;
+ while (e.hasMoreElements()) {
+ perm2[i] = (FilePermission) e.nextElement();
+ i++;
+ }
+ assertEquals("Permissions.elements did not return the correct number "
+ + "of permission - called in add() test ", i, perm.length);
+ }
+
+ /**
+ * @tests java.security.Permissions#elements()
+ */
+ public void test_elements() {
+ // Test for method java.util.Enumeration
+ // java.security.Permissions.elements()
+ char s = File.separatorChar;
+ FilePermission perm[] = new FilePermission[7];
+ perm[0] = readAllFiles;
+ perm[1] = allInCurrent;
+ perm[2] = new FilePermission(s + "tmp" + s + "test" + s + "*",
+ "read,write");
+ perm[3] = new FilePermission(s + "tmp" + s + "test" + s
+ + "collection.file", "read");
+ perm[4] = alsoReadAllFiles;
+ perm[5] = readInFile;
+ perm[6] = new FilePermission("hello.file", "write");
+ Permissions perms = new Permissions();
+ for (int i = 0; i < perm.length; i++) {
+ perms.add(perm[i]);
+ }
+ Enumeration e = perms.elements();
+ FilePermission perm2[] = new FilePermission[10];
+ int i = 0;
+ while (e.hasMoreElements()) {
+ perm2[i] = (FilePermission) e.nextElement();
+ i++;
+ }
+ assertEquals("Permissions.elements did not return the correct "
+ + "number of permission - called in element() test", i,
+ perm.length);
+ }
+
+ /**
+ * @tests java.security.Permissions#implies(java.security.Permission)
+ */
+ public void test_impliesLjava_security_Permission() {
+ // Test for method boolean
+ // java.security.Permissions.implies(java.security.Permission)
+ char s = File.separatorChar;
+ FilePermission perm[] = new FilePermission[7];
+ perm[0] = new FilePermission("test1.file", "write");
+ perm[1] = allInCurrent;
+ perm[2] = new FilePermission(s + "tmp" + s + "test" + s + "*",
+ "read,write");
+ perm[3] = new FilePermission(s + "tmp" + s + "test" + s
+ + "collection.file", "read");
+ perm[4] = new FilePermission(s + "windows" + "*", "delete");
+ perm[5] = readInFile;
+ perm[6] = new FilePermission("hello.file", "write");
+ Permissions perms = new Permissions();
+ for (int i = 0; i < perm.length; i++) {
+ perms.add(perm[i]);
+ }
+ assertTrue("Returned true for non-subset of files", !perms
+ .implies(new FilePermission("<<ALL FILES>>", "execute")));
+ assertTrue("Returned true for non-subset of action", !perms
+ .implies(new FilePermission(s + "tmp" + s + "test" + s + "*",
+ "execute")));
+ assertTrue("Returned false for subset of actions", perms
+ .implies(new FilePermission("*", "write")));
+ assertTrue("Returned false for subset of files", perms
+ .implies(new FilePermission(s + "tmp" + s + "test" + s
+ + "test.file", "read")));
+ assertTrue("Returned false for subset of files and actions", perms
+ .implies(new FilePermission(s + "tmp" + s + "test" + s
+ + "test2.file", "write")));
+ }
+
+ /**
+ * 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