@Test public void testBuildDefault() throws Exception { new SSLContextBuilder().build(); }
@Test(expected=KeyStoreException.class) public void testBuildNoSuchKeyStoreType() throws Exception { final URL resource1 = getResource("/test-keypasswd.keystore"); final String storePassword = "nopassword"; final String keyPassword = "password"; SSLContextBuilder.create() .setKeyStoreType(" BAD ") .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); }
@Test public void testBuildWithProvider() throws Exception { final URL resource1 = getResource("/test-server.keystore"); final String storePassword = "nopassword"; final String keyPassword = "nopassword"; final SSLContext sslContext=SSLContextBuilder.create() .setProvider(Security.getProvider(PROVIDER_SUN_JSSE)) .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertEquals(PROVIDER_SUN_JSSE, sslContext.getProvider().getName()); }
@Test(expected=NoSuchAlgorithmException.class) public void testBuildNoSuchKeyManagerFactoryAlgorithm() throws Exception { final URL resource1 = getResource("/test-keypasswd.keystore"); final String storePassword = "nopassword"; final String keyPassword = "password"; SSLContextBuilder.create() .setKeyManagerFactoryAlgorithm(" BAD ") .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); }
@Test public void testKeyWithAlternatePasswordInvalid() throws Exception { thrown.expect(UnrecoverableKeyException.class); final URL resource1 = getResource("/test-keypasswd.keystore"); final String storePassword = "nopassword"; final String keyPassword = "!password"; SSLContextBuilder.create() .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .loadTrustMaterial(resource1, storePassword.toCharArray()) .build(); }
@Test public void testKeyWithAlternatePassword() throws Exception { final URL resource1 = getResource("/test-keypasswd.keystore"); final String storePassword = "nopassword"; final String keyPassword = "password"; final SSLContext sslContext = SSLContextBuilder.create() .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .loadTrustMaterial(resource1, storePassword.toCharArray()) .build(); Assert.assertNotNull(sslContext); final SSLSocketFactory socketFactory = sslContext.getSocketFactory(); Assert.assertNotNull(socketFactory); }
@Test public void testBuildWithProviderName() throws Exception { final URL resource1 = getResource("/test-server.keystore"); final String storePassword = "nopassword"; final String keyPassword = "nopassword"; final SSLContext sslContext=SSLContextBuilder.create() .setProvider(PROVIDER_SUN_JSSE) .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertEquals(PROVIDER_SUN_JSSE, sslContext.getProvider().getName()); }
@Test(expected=NoSuchAlgorithmException.class) public void testBuildNoSuchTrustManagerFactoryAlgorithm() throws Exception { final URL resource1 = getResource("/test-keypasswd.keystore"); final String storePassword = "nopassword"; SSLContextBuilder.create() .setTrustManagerFactoryAlgorithm(" BAD ") .loadTrustMaterial(resource1, storePassword.toCharArray()) .build(); }
final SSLContext serverSslContext = SSLContextBuilder.create() .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertNotNull(serverSslContext); final SSLContext clientSslContext = SSLContextBuilder.create() .loadTrustMaterial(resource1, storePassword.toCharArray()) .build(); Assert.assertNotNull(clientSslContext); final ServerSocket serverSocket = serverSslContext.getServerSocketFactory().createServerSocket();
final SSLContext serverSslContext = SSLContextBuilder.create() .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertNotNull(serverSslContext); final URL resource2 = getResource("/test.keystore"); final SSLContext clientSslContext = SSLContextBuilder.create() .loadTrustMaterial(resource2, storePassword.toCharArray()) .build(); Assert.assertNotNull(clientSslContext); final ServerSocket serverSocket = serverSslContext.getServerSocketFactory().createServerSocket();
final SSLContext serverSslContext = SSLContextBuilder.create() .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertNotNull(serverSslContext); final URL resource2 = getResource("/test-client.keystore"); final SSLContext clientSslContext = SSLContextBuilder.create() .loadTrustMaterial(resource2, storePassword.toCharArray()) .build(); Assert.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket();
final SSLContext serverSslContext = SSLContextBuilder.create() .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertNotNull(serverSslContext); final URL resource2 = getResource("/test-client.keystore"); final SSLContext clientSslContext = SSLContextBuilder.create() .loadTrustMaterial(resource2, storePassword.toCharArray()) .build(); Assert.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket();
.loadTrustMaterial(resource1, storePassword.toCharArray()) .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertNotNull(serverSslContext); final URL resource2 = getResource("/test-client.keystore"); .loadTrustMaterial(resource2, storePassword.toCharArray()) .loadKeyMaterial(resource2, storePassword.toCharArray(), storePassword.toCharArray()) .build(); Assert.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket();
final SSLContext serverSslContext = SSLContextBuilder.create() .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertNotNull(serverSslContext); final URL resource2 = getResource("/test-client.keystore"); final SSLContext clientSslContext = SSLContextBuilder.create() .loadTrustMaterial(resource2, storePassword.toCharArray()) .build(); Assert.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket();
final SSLContext serverSslContext = SSLContextBuilder.create() .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertNotNull(serverSslContext); final URL resource2 = getResource("/test-client.keystore"); final SSLContext clientSslContext = SSLContextBuilder.create() .loadTrustMaterial(resource2, storePassword.toCharArray()) .build(); Assert.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket();
.loadTrustMaterial(resource1, storePassword.toCharArray()) .loadKeyMaterial(resource1, storePassword.toCharArray(), keyPassword.toCharArray()) .build(); Assert.assertNotNull(serverSslContext); .loadTrustMaterial(resource2, storePassword.toCharArray()) .loadKeyMaterial(resource2, storePassword.toCharArray(), storePassword.toCharArray(), privateKeyStrategy) .build(); Assert.assertNotNull(clientSslContext); final SSLServerSocket serverSocket = (SSLServerSocket) serverSslContext.getServerSocketFactory().createServerSocket();
@Test public void testBuildAllDefaults() throws Exception { final SSLContext sslContext = SSLContextBuilder.create() .setKeyStoreType(KeyStore.getDefaultType()) .setKeyManagerFactoryAlgorithm(KeyManagerFactory.getDefaultAlgorithm()) .setTrustManagerFactoryAlgorithm(TrustManagerFactory.getDefaultAlgorithm()) .setProvider(PROVIDER_SUN_JSSE) .setProtocol("TLS") .setSecureRandom(null) .loadTrustMaterial((KeyStore) null, null) .loadKeyMaterial((KeyStore) null, null, null) .build(); Assert.assertNotNull(sslContext); Assert.assertEquals("TLS", sslContext.getProtocol()); Assert.assertEquals(PROVIDER_SUN_JSSE, sslContext.getProvider().getName()); }
@Test public void testBuildAllNull_deprecated() throws Exception { final SSLContext sslContext = SSLContextBuilder.create() .setProtocol(null) .setSecureRandom(null) .loadTrustMaterial((KeyStore) null, null) .loadKeyMaterial((KeyStore) null, null, null) .build(); Assert.assertNotNull(sslContext); Assert.assertEquals("TLS", sslContext.getProtocol()); }
@Test public void testBuildAllNull() throws Exception { final SSLContext sslContext = SSLContextBuilder.create() .setKeyStoreType(null) .setKeyManagerFactoryAlgorithm(null) .setTrustManagerFactoryAlgorithm(null) .setProtocol(null) .setProvider((String) null) .setSecureRandom(null) .loadTrustMaterial((KeyStore) null, null) .loadKeyMaterial((KeyStore) null, null, null) .build(); Assert.assertNotNull(sslContext); Assert.assertEquals("TLS", sslContext.getProtocol()); Assert.assertEquals(PROVIDER_SUN_JSSE, sslContext.getProvider().getName()); }