use of java.security.KeyStoreException in project robovm by robovm.
the class KeyStoreBuilderTest method testNewInstanceKeyStoreProtectionParameter.
/*
* test for method newInstance(KeyStore, KeyStore.ProtectionParameter)
*/
public void testNewInstanceKeyStoreProtectionParameter() throws KeyStoreException, NoSuchAlgorithmException, IOException, CertificateException {
try {
KeyStore.Builder.newInstance(null, null);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
// expected
}
try {
KeyStore.Builder.newInstance(null, protPass);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
// expected
}
KeyStore.Builder ksB;
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
try {
KeyStore.Builder.newInstance(ks, null);
fail("NullPointerException must be thrown when ProtectionParameter is null");
} catch (NullPointerException e) {
// expected
}
KeyStore.PasswordProtection protPass1 = new KeyStore.PasswordProtection(pass);
KeyStore.ProtectionParameter[] pp = { protPass, protPass1, callbackHand, myProtParam };
for (int i = 0; i < pp.length; i++) {
ks = KeyStore.getInstance(KeyStore.getDefaultType());
try {
KeyStore.Builder.newInstance(ks, pp[i]);
fail("IllegalArgumentException must be thrown because KeyStore was not initialized");
} catch (IllegalArgumentException e) {
// expected
}
ks.load(null, pass);
ksB = KeyStore.Builder.newInstance(ks, pp[i]);
assertEquals("Incorrect KeyStore", ksB.getKeyStore().size(), 0);
ksB = KeyStore.Builder.newInstance(ks, pp[i]);
// verification getKeyStore() and getProtectionParameter(String
// alias)
assertEquals("Incorrect KeyStore", ks, ksB.getKeyStore());
try {
ksB.getProtectionParameter(null);
fail("NullPointerException must be thrown");
} catch (NullPointerException e) {
}
try {
assertEquals(ksB.getProtectionParameter("aaa"), pp[i]);
} catch (KeyStoreException e) {
fail("Unexpected: " + e.toString() + " was thrown");
}
try {
assertEquals(ksB.getProtectionParameter("Bad alias"), pp[i]);
} catch (KeyStoreException e) {
// KeyStoreException might be thrown because there is no entry
// with such alias
}
try {
assertEquals(ksB.getProtectionParameter(""), pp[i]);
} catch (KeyStoreException e) {
// KeyStoreException might be thrown because there is no entry
// with such alias
}
KeyStore.ProtectionParameter pPar = ksB.getProtectionParameter("aaa");
switch(i) {
case 0:
assertTrue(pPar instanceof KeyStore.PasswordProtection);
break;
case 1:
assertTrue(pPar instanceof KeyStore.PasswordProtection);
break;
case 2:
assertTrue(pPar instanceof KeyStore.CallbackHandlerProtection);
break;
case 3:
assertTrue(pPar instanceof MyProtectionParameter);
break;
default:
fail("Incorrect protection parameter");
}
assertEquals(pPar, pp[i]);
}
}
use of java.security.KeyStoreException in project robovm by robovm.
the class KeyStoreBuilderTest method testNewInstanceStringProviderFileProtectionParameter.
/*
* Test for methods: <code>newInstance(String type, Provider provider, File
* file, ProtectionParameter protectionParameter)</code> <code>getKeyStore()</code>
* <code>getProtectionParameter(String alias)</code> Assertions: throws
* NullPointerException if type, file or protectionParameter is null; throws
* IllegalArgumentException if file does not exist or is not file; throws
* IllegalArgumentException if ProtectionParameter is not PasswordProtection
* or CallbackHandlerProtection; returns new object
*
* getKeyStore() returns specified keystore; getProtectionParameter(String
* alias) throws NullPointerException when alias is null; throws
* KeyStoreException when alias is not available; returns
* ProtectionParameter which is used in newInstance(...)
*
*/
public void testNewInstanceStringProviderFileProtectionParameter() throws Exception {
File fl = File.createTempFile("KSBuilder_ImplTest", "keystore");
fl.deleteOnExit();
KeyStore.Builder ksB;
KeyStore.Builder ksB1;
KeyStore ks = null;
KeyStore ks1 = null;
MyProtectionParameter myPP = new MyProtectionParameter(new byte[5]);
// check exceptions
try {
KeyStore.Builder.newInstance(null, defaultProvider, fl, protPass);
fail("NullPointerException must be thrown when type is null");
} catch (NullPointerException e) {
}
try {
KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, null, protPass);
fail("NullPointerException must be thrown when file is null");
} catch (NullPointerException e) {
}
try {
KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, null);
fail("NullPointerException must be thrown when ProtectionParameter is null");
} catch (NullPointerException e) {
}
try {
KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, myPP);
fail("IllegalArgumentException must be thrown when ProtectionParameter is not correct");
} catch (IllegalArgumentException e) {
}
try {
KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, new File(fl.getAbsolutePath().concat("should_absent")), protPass);
fail("IllegalArgumentException must be thrown when file does not exist");
} catch (IllegalArgumentException e) {
}
try {
// 'file' param points to directory
KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl.getParentFile(), protPass);
fail("IllegalArgumentException must be thrown when file does not exist");
} catch (IllegalArgumentException e) {
}
ksB = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, protPass);
try {
ksB.getKeyStore();
fail("KeyStoreException must be throw because file is empty");
} catch (KeyStoreException e) {
}
fl = createKS();
// Exception Tests with custom ProtectionParameter
try {
KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, fl, myPP);
fail("IllegalArgumentException must be " + "thrown for incorrect ProtectionParameter");
} catch (IllegalArgumentException e) {
}
try {
KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, myPP);
fail("IllegalArgumentException must be " + "thrown for incorrect ProtectionParameter");
} catch (IllegalArgumentException e) {
}
// Tests with PasswordProtection
ksB = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, fl, protPass);
ksB1 = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, protPass);
try {
ks = ksB.getKeyStore();
} catch (KeyStoreException e) {
fail("Unexpected KeyException was thrown");
}
try {
ks1 = ksB1.getKeyStore();
} catch (KeyStoreException e) {
fail("Unexpected KeyException was thrown: " + e.getMessage());
}
assertEquals("Incorrect KeyStore size", ks.size(), ks1.size());
;
for (Enumeration<String> aliases = ks.aliases(); aliases.hasMoreElements(); ) {
String aName = aliases.nextElement();
try {
assertEquals("Incorrect ProtectionParameter", ksB.getProtectionParameter(aName), protPass);
} catch (Exception e) {
fail("Unexpected: " + e.toString() + " was thrown for alias: " + aName);
}
}
ksB.getKeyStore();
try {
assertEquals(ksB.getProtectionParameter("Bad alias"), protPass);
} catch (KeyStoreException e) {
// KeyStoreException might be thrown because there is no entry
// with such alias
}
for (Enumeration<String> aliases = ks1.aliases(); aliases.hasMoreElements(); ) {
String aName = aliases.nextElement();
assertEquals("Incorrect ProtectionParameter", ksB1.getProtectionParameter(aName), protPass);
}
try {
assertEquals(ksB1.getProtectionParameter("Bad alias"), protPass);
} catch (KeyStoreException e) {
// KeyStoreException might be thrown because there is no entry
// with such alias
}
// Tests with CallbackHandlerProtection
ksB = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, fl, callbackHand);
ksB1 = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, callbackHand);
try {
ks = ksB.getKeyStore();
fail("KeyStoreException must be thrown for incorrect " + "ProtectionParameter");
} catch (KeyStoreException e) {
}
try {
ks1 = ksB1.getKeyStore();
fail("KeyStoreException must be thrown for incorrect " + "ProtectionParameter");
} catch (KeyStoreException e) {
}
assertEquals("Incorrect KeyStore size", ks.size(), ks1.size());
for (Enumeration<String> aliases = ks.aliases(); aliases.hasMoreElements(); ) {
String aName = aliases.nextElement();
try {
assertEquals("Incorrect ProtectionParameter", ksB.getProtectionParameter(aName), callbackHand);
} catch (Exception e) {
fail("Unexpected: " + e.toString() + " was thrown for alias: " + aName);
}
}
for (Enumeration<String> iter = ks1.aliases(); iter.hasMoreElements(); ) {
String aName = iter.nextElement();
assertEquals("Incorrect ProtectionParameter", ksB1.getProtectionParameter(aName), callbackHand);
}
}
use of java.security.KeyStoreException in project robovm by robovm.
the class KeyStore2Test method test_getCertificateLjava_lang_String.
/**
* java.security.KeyStore#getCertificate(java.lang.String)
*/
public void test_getCertificateLjava_lang_String() throws Exception {
// Test for method java.security.cert.Certificate
// java.security.KeyStore.getCertificate(java.lang.String)
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());
try {
keyTest.getCertificate("anAlias");
fail();
} catch (KeyStoreException expected) {
}
keyTest.load(null, null);
// alias 1
PublicKey pub = cert[0].getPublicKey();
keyTest.setCertificateEntry("alias1", cert[0]);
Certificate certRes = keyTest.getCertificate("alias1");
assertEquals("the public key of the certificate from getCertificate() " + "did not equal the original certificate", pub, certRes.getPublicKey());
// alias 2
keyTest.setCertificateEntry("alias2", cert[0]);
// testing for a certificate chain
Certificate cert2 = keyTest.getCertificate("alias2");
assertEquals("the certificate for alias2 is supposed to exist", cert2, cert[0]);
}
use of java.security.KeyStoreException in project robovm by robovm.
the class KeyStore2Test method test_containsAliasLjava_lang_String.
/**
* java.security.KeyStore#containsAlias(java.lang.String)
*/
public void test_containsAliasLjava_lang_String() throws Exception {
// Test for method boolean
// java.security.KeyStore.containsAlias(java.lang.String)
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());
try {
keyTest.containsAlias("alias1");
fail();
} catch (KeyStoreException expected) {
}
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"));
try {
keyTest.containsAlias(null);
fail();
} catch (NullPointerException expected) {
}
}
use of java.security.KeyStoreException in project robovm by robovm.
the class KeyStore2Test method test_size.
/**
* java.security.KeyStore#size()
*/
public void test_size() throws Exception {
// Test for method int java.security.KeyStore.size()
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());
try {
keyTest.size();
fail();
} catch (KeyStoreException expected) {
}
keyTest.load(null, null);
// alias 1
keyTest.setCertificateEntry("alias1", cert[0]);
// alias 2
keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert);
// alias 3
keyTest.setCertificateEntry("alias3", cert[1]);
assertEquals("the size of the keyStore is not 3", 3, keyTest.size());
}
Aggregations