summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStoreSpiTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStoreSpiTest.java')
-rw-r--r--luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStoreSpiTest.java99
1 files changed, 49 insertions, 50 deletions
diff --git a/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStoreSpiTest.java b/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStoreSpiTest.java
index 4fdddbb..a85459b 100644
--- a/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStoreSpiTest.java
+++ b/luni/src/test/java/org/apache/harmony/security/tests/java/security/KeyStoreSpiTest.java
@@ -15,21 +15,15 @@
* limitations under the License.
*/
-/**
- * @author Vera Y. Petrashkova
- * @version $Revision$
- */
-
package org.apache.harmony.security.tests.java.security;
-import junit.framework.TestCase;
-
-import org.apache.harmony.security.tests.support.MyKeyStoreSpi;
-import org.apache.harmony.security.tests.support.MyLoadStoreParams;
-
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
+import java.security.Key;
+import java.security.KeyStore.LoadStoreParameter;
+import java.security.KeyStore.Entry;
+import java.security.KeyStore.ProtectionParameter;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.KeyStoreSpi;
@@ -39,16 +33,15 @@ import java.security.PublicKey;
import java.security.SignatureException;
import java.security.UnrecoverableEntryException;
import java.security.UnrecoverableKeyException;
-import java.security.KeyStore.LoadStoreParameter;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.util.Date;
+import javax.crypto.SecretKey;
+import junit.framework.TestCase;
+import org.apache.harmony.security.tests.support.MyKeyStoreSpi;
+import org.apache.harmony.security.tests.support.MyLoadStoreParams;
-/**
- * Tests for <code>KeyStoreSpi</code> constructor and methods
- *
- */
public class KeyStoreSpiTest extends TestCase {
@SuppressWarnings("cast")
@@ -86,62 +79,73 @@ public class KeyStoreSpiTest extends TestCase {
try {
assertFalse(ksSpi.engineEntryInstanceOf(null,
KeyStore.TrustedCertificateEntry.class));
- } catch (NullPointerException e) {
- // ok
+ } catch (NullPointerException expected) {
}
try {
assertFalse(ksSpi.engineEntryInstanceOf(
"test_engineEntryInstanceOf_Alias1", null));
- } catch (NullPointerException e) {
- // ok
+ } catch (NullPointerException expected) {
}
}
- public void testKeyStoteSpi01() throws IOException,
+ public void testKeyStoreSpi01() throws IOException,
NoSuchAlgorithmException, CertificateException,
UnrecoverableEntryException, KeyStoreException {
- KeyStoreSpi ksSpi = new MyKeyStoreSpi();
+ final boolean[] keyEntryWasSet = new boolean[1];
+ KeyStoreSpi ksSpi = new MyKeyStoreSpi() {
+ @Override public void engineSetKeyEntry(String alias, Key key, char[] password,
+ Certificate[] chain) throws KeyStoreException { keyEntryWasSet[0] = true; }
+ };
+
+ BadKeyStoreEntry badEntry = new BadKeyStoreEntry();
+ BadKeyStoreProtectionParameter badParameter = new BadKeyStoreProtectionParameter();
- tmpEntry entry = new tmpEntry();
- tmpProtection pPar = new tmpProtection();
+ KeyStore.SecretKeyEntry dummyEntry = new KeyStore.SecretKeyEntry(new SecretKey() {
+ @Override public String getAlgorithm() { return null; }
+ @Override public String getFormat() { return null; }
+ @Override public byte[] getEncoded() { return null; }
+ });
try {
ksSpi.engineStore(null);
- } catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException expected) {
}
assertNull("Not null entry", ksSpi.engineGetEntry("aaa", null));
- assertNull("Not null entry", ksSpi.engineGetEntry(null, pPar));
- assertNull("Not null entry", ksSpi.engineGetEntry("aaa", pPar));
+ assertNull("Not null entry", ksSpi.engineGetEntry(null, badParameter));
+ assertNull("Not null entry", ksSpi.engineGetEntry("aaa", badParameter));
try {
ksSpi.engineSetEntry("", null, null);
fail("KeyStoreException or NullPointerException must be thrown");
- } catch (KeyStoreException e) {
- } catch (NullPointerException e) {
+ } catch (KeyStoreException expected) {
+ } catch (NullPointerException expected) {
}
try {
ksSpi.engineSetEntry("", new KeyStore.TrustedCertificateEntry(
new MyCertificate("type", new byte[0])), null);
fail("KeyStoreException must be thrown");
- } catch (KeyStoreException e) {
+ } catch (KeyStoreException expected) {
}
try {
- ksSpi.engineSetEntry("aaa", entry, null);
+ ksSpi.engineSetEntry("aaa", badEntry, null);
fail("KeyStoreException must be thrown");
- } catch (KeyStoreException e) {
+ } catch (KeyStoreException expected) {
}
+
+ ksSpi.engineSetEntry("aaa", dummyEntry, null);
+ assertTrue(keyEntryWasSet[0]);
}
/**
* Test for <code>KeyStoreSpi()</code> constructor and abstract engine
* methods. Assertion: creates new KeyStoreSpi object.
*/
- public void testKeyStoteSpi02() throws NoSuchAlgorithmException,
+ public void testKeyStoreSpi02() throws NoSuchAlgorithmException,
UnrecoverableKeyException, CertificateException {
KeyStoreSpi ksSpi = new MyKeyStoreSpi();
assertNull("engineGetKey(..) must return null", ksSpi.engineGetKey("",
@@ -155,23 +159,23 @@ public class KeyStoreSpiTest extends TestCase {
try {
ksSpi.engineSetKeyEntry("", null, new char[0], new Certificate[0]);
fail("KeyStoreException must be thrown from engineSetKeyEntry(..)");
- } catch (KeyStoreException e) {
+ } catch (KeyStoreException expected) {
}
try {
ksSpi.engineSetKeyEntry("", new byte[0], new Certificate[0]);
fail("KeyStoreException must be thrown from engineSetKeyEntry(..)");
- } catch (KeyStoreException e) {
+ } catch (KeyStoreException expected) {
}
try {
ksSpi.engineSetCertificateEntry("", null);
fail("KeyStoreException must be thrown "
+ "from engineSetCertificateEntry(..)");
- } catch (KeyStoreException e) {
+ } catch (KeyStoreException expected) {
}
try {
ksSpi.engineDeleteEntry("");
fail("KeyStoreException must be thrown from engineDeleteEntry(..)");
- } catch (KeyStoreException e) {
+ } catch (KeyStoreException expected) {
}
assertNull("engineAliases() must return null", ksSpi.engineAliases());
assertFalse("engineContainsAlias(..) must return false", ksSpi
@@ -180,7 +184,7 @@ public class KeyStoreSpiTest extends TestCase {
try {
ksSpi.engineStore(null, null);
fail("IOException must be thrown");
- } catch (IOException e) {
+ } catch (IOException expected) {
}
}
@@ -202,35 +206,30 @@ public class KeyStoreSpiTest extends TestCase {
try {
ksSpi.engineLoad(null);
fail("Should throw exception");
- } catch (RuntimeException e) {
- assertSame(msg, e.getMessage());
+ } catch (RuntimeException expected) {
+ assertSame(msg, expected.getMessage());
}
// test: protection parameter is null
try {
ksSpi.engineLoad(new MyLoadStoreParams(null));
fail("No expected UnsupportedOperationException");
- } catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException expected) {
}
// test: protection parameter is not instanceof
// PasswordProtection or CallbackHandlerProtection
try {
- ksSpi.engineLoad(new MyLoadStoreParams(new tmpProtection()));
+ ksSpi.engineLoad(new MyLoadStoreParams(new BadKeyStoreProtectionParameter()));
fail("No expected UnsupportedOperationException");
- } catch (UnsupportedOperationException e) {
+ } catch (UnsupportedOperationException expected) {
}
}
}
-/**
- * Additional class implements KeyStore.Entry interface
- */
-class tmpEntry implements KeyStore.Entry {
-}
-
-class tmpProtection implements KeyStore.ProtectionParameter {
-}
+// These are "Bad" because they are not expected inner subclasses of the KeyStore class.
+class BadKeyStoreEntry implements Entry {}
+class BadKeyStoreProtectionParameter implements ProtectionParameter {}
@SuppressWarnings("unused")
class MyCertificate extends Certificate {