use of java.security.Provider in project robovm by robovm.
the class MyProvider method test_engineInit_02.
/**
* @throws InvalidAlgorithmParameterException
* @throws NoSuchAlgorithmException
* javax.net.ssl.TrustManagerFactorySpi#engineInit(ManagerFactoryParameters spec)
*/
public void test_engineInit_02() throws InvalidAlgorithmParameterException, NoSuchAlgorithmException {
factory.reset();
Provider provider = new MyProvider();
TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF", provider);
Parameters pr = null;
try {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
pr = new Parameters(ks);
tmf.init(pr);
} catch (Exception e) {
fail("Unexpected exception " + e.toString());
}
assertTrue(factory.isEngineInitCalled());
assertEquals(pr, factory.getSpec());
factory.reset();
tmf.init((ManagerFactoryParameters) null);
assertTrue(factory.isEngineInitCalled());
assertNull(factory.getSpec());
}
use of java.security.Provider in project robovm by robovm.
the class MyProvider method test_engineGetTrustManagers.
/**
* @throws NoSuchAlgorithmException
* javax.net.ssl.TrustManagerFactorySpi#engineGetTrustManagers()
*/
public void test_engineGetTrustManagers() throws NoSuchAlgorithmException {
factory.reset();
Provider provider = new MyProvider();
TrustManagerFactory tmf = TrustManagerFactory.getInstance("MyTMF", provider);
TrustManager[] tm = tmf.getTrustManagers();
assertTrue(factory.isEngineGetTrustManagersCalled());
factory.reset();
try {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
tmf.init(ks);
tm = tmf.getTrustManagers();
assertTrue(factory.isEngineGetTrustManagersCalled());
} catch (Exception e) {
fail("Unexpected exception " + e.toString());
}
}
use of java.security.Provider in project robovm by robovm.
the class TrustManagerFactory2Test method testLjava_lang_StringLjava_security_Provider.
/**
* Test for <code>getInstance(String algorithm, Provider provider)</code>
* method
* Assertions:
* throws NullPointerException when algorithm is null;
* throws NoSuchAlgorithmException when algorithm is not correct;
* throws IllegalArgumentException when provider is null;
* returns TrustManagerFactory object
*/
public void testLjava_lang_StringLjava_security_Provider() throws Exception {
try {
TrustManagerFactory.getInstance(null, mProv);
fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
} catch (NoSuchAlgorithmException e) {
} catch (NullPointerException e) {
}
for (int i = 0; i < invalidValues.length; i++) {
try {
TrustManagerFactory.getInstance(invalidValues[i], mProv);
fail("NoSuchAlgorithmException must be thrown (algorithm: ".concat(invalidValues[i]).concat(")"));
} catch (NoSuchAlgorithmException e) {
}
}
Provider prov = null;
for (int i = 0; i < validValues.length; i++) {
try {
TrustManagerFactory.getInstance(validValues[i], prov);
fail("IllegalArgumentException must be thrown when provider is null (algorithm: ".concat(invalidValues[i]).concat(")"));
} catch (IllegalArgumentException e) {
}
}
TrustManagerFactory tmf;
for (int i = 0; i < validValues.length; i++) {
tmf = TrustManagerFactory.getInstance(validValues[i], mProv);
assertTrue("Not instanceof TrustManagerFactory object", tmf instanceof TrustManagerFactory);
assertEquals("Incorrect algorithm", tmf.getAlgorithm(), validValues[i]);
assertEquals("Incorrect provider", tmf.getProvider(), mProv);
checkResult(tmf);
}
}
use of java.security.Provider in project robovm by robovm.
the class ProviderTest method test_Provider_Properties.
/**
* Makes sure all provider properties either point to a class
* implementation that exists or are aliases to known algorithms.
*/
public void test_Provider_Properties() throws Exception {
/*
* A useful reference on Provider properties
* <a href="http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/HowToImplAProvider.html>
* How to Implement a Provider in the Java ™ Cryptography Architecture
* </a>
*/
Provider[] providers = Security.getProviders();
for (Provider provider : providers) {
// check Provider.id proprieties
assertEquals(provider.getName(), provider.get("Provider.id name"));
assertEquals(String.valueOf(provider.getVersion()), provider.get("Provider.id version"));
assertEquals(provider.getInfo(), provider.get("Provider.id info"));
assertEquals(provider.getClass().getName(), provider.get("Provider.id className"));
// build map of all known aliases and implementations
Map<String, String> aliases = new HashMap<String, String>();
Map<String, String> implementations = new HashMap<String, String>();
for (Entry<Object, Object> entry : provider.entrySet()) {
Object k = entry.getKey();
Object v = entry.getValue();
assertEquals(String.class, k.getClass());
assertEquals(String.class, v.getClass());
String key = (String) k;
String value = (String) v;
// skip Provider.id keys, we check well known ones values above
if (key.startsWith("Provider.id ")) {
continue;
}
// skip property settings such as: "Signature.SHA1withDSA ImplementedIn" "Software"
if (key.indexOf(' ') != -1) {
continue;
}
Matcher m = alias.matcher(key);
if (m.find()) {
String type = m.group(1);
aliases.put(key, type + "." + value);
} else {
implementations.put(key, value);
}
}
// verify implementation classes are available
for (Entry<String, String> entry : implementations.entrySet()) {
String typeAndAlgorithm = entry.getKey();
String className = entry.getValue();
try {
assertNotNull(Class.forName(className, true, provider.getClass().getClassLoader()));
} catch (ClassNotFoundException e) {
// Sun forgot their own class
if (!className.equals("sun.security.pkcs11.P11MAC")) {
fail("Could not find class " + className + " for " + typeAndAlgorithm);
}
}
}
// make sure all aliases point to some known implementation
for (Entry<String, String> entry : aliases.entrySet()) {
String alias = entry.getKey();
String actual = entry.getValue();
assertTrue("Could not find implementation " + actual + " for alias " + alias, implementations.containsKey(actual));
}
}
}
use of java.security.Provider in project robovm by robovm.
the class SecureRandomTest method test_getInstance.
public void test_getInstance() throws Exception {
Provider[] providers = Security.getProviders();
for (Provider provider : providers) {
Set<Provider.Service> services = provider.getServices();
for (Provider.Service service : services) {
String type = service.getType();
if (!type.equals("SecureRandom")) {
continue;
}
String algorithm = service.getAlgorithm();
try {
SecureRandom sr1 = SecureRandom.getInstance(algorithm);
assertEquals(algorithm, sr1.getAlgorithm());
test_SecureRandom(sr1);
// SecureRandom.getInstance(String, Provider)
SecureRandom sr2 = SecureRandom.getInstance(algorithm, provider);
assertEquals(algorithm, sr2.getAlgorithm());
assertEquals(provider, sr2.getProvider());
test_SecureRandom(sr2);
// SecureRandom.getInstance(String, String)
SecureRandom sr3 = SecureRandom.getInstance(algorithm, provider.getName());
assertEquals(algorithm, sr3.getAlgorithm());
assertEquals(provider, sr3.getProvider());
test_SecureRandom(sr3);
System.out.println("SecureRandomTest " + algorithm + " and provider " + provider.getName());
} catch (Exception e) {
throw new Exception("Problem testing SecureRandom." + algorithm + ", provider: " + provider.getName(), e);
}
}
}
}
Aggregations