Tab police and use // @formatter:[off|on]
git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1783963 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f77bdff541
commit
fcbfd1d914
|
@ -89,76 +89,83 @@ public class TestSSLSocketFactory {
|
|||
|
||||
@Test
|
||||
public void testBasicSSL() throws Exception {
|
||||
// @formatter:off
|
||||
this.server = ServerBootstrap.bootstrap()
|
||||
.setSslContext(SSLTestContexts.createServerSSLContext())
|
||||
.create();
|
||||
// @formatter:on
|
||||
this.server.start();
|
||||
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext(), hostVerifier);
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicDefaultHostnameVerifier() throws Exception {
|
||||
// @formatter:off
|
||||
this.server = ServerBootstrap.bootstrap()
|
||||
.setSslContext(SSLTestContexts.createServerSSLContext())
|
||||
.create();
|
||||
// @formatter:on
|
||||
this.server.start();
|
||||
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext(), SSLConnectionSocketFactory.getDefaultHostnameVerifier());
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
|
||||
Assert.assertNotNull(sslsession);
|
||||
}
|
||||
}
|
||||
}
|
||||
Assert.assertNotNull(sslsession);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientAuthSSL() throws Exception {
|
||||
// @formatter:off
|
||||
this.server = ServerBootstrap.bootstrap()
|
||||
.setSslContext(SSLTestContexts.createServerSSLContext())
|
||||
.create();
|
||||
// @formatter:on
|
||||
this.server.start();
|
||||
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext(), hostVerifier);
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
}
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=IOException.class)
|
||||
@Test(expected = IOException.class)
|
||||
public void testClientAuthSSLFailure() throws Exception {
|
||||
// @formatter:off
|
||||
this.server = ServerBootstrap.bootstrap()
|
||||
.setSslContext(SSLTestContexts.createServerSSLContext())
|
||||
.setSslSetupHandler(new SSLServerSetupHandler() {
|
||||
|
@ -170,30 +177,33 @@ public class TestSSLSocketFactory {
|
|||
|
||||
})
|
||||
.create();
|
||||
// @formatter:on
|
||||
this.server.start();
|
||||
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext(), hostVerifier);
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
|
||||
context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
}
|
||||
Assert.assertNotNull(sslsession);
|
||||
Assert.assertTrue(hostVerifier.isFired());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=SSLException.class)
|
||||
@Test(expected = SSLException.class)
|
||||
public void testSSLTrustVerification() throws Exception {
|
||||
// @formatter:off
|
||||
this.server = ServerBootstrap.bootstrap()
|
||||
.setSslContext(SSLTestContexts.createServerSSLContext())
|
||||
.create();
|
||||
// @formatter:on
|
||||
this.server.start();
|
||||
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
|
@ -203,23 +213,22 @@ public class TestSSLSocketFactory {
|
|||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(defaultsslcontext,
|
||||
NoopHostnameVerifier.INSTANCE);
|
||||
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
// empty for now
|
||||
}
|
||||
}
|
||||
}
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
// empty for now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSSLTrustVerificationOverrideWithCustsom() throws Exception {
|
||||
final TrustStrategy trustStrategy = new TrustStrategy() {
|
||||
|
||||
@Override
|
||||
public boolean isTrusted(
|
||||
final X509Certificate[] chain, final String authType) throws CertificateException {
|
||||
public boolean isTrusted(final X509Certificate[] chain, final String authType) throws CertificateException {
|
||||
return chain.length == 1;
|
||||
}
|
||||
|
||||
|
@ -237,34 +246,38 @@ public class TestSSLSocketFactory {
|
|||
testSSLTrustVerificationOverride(TrustAllStrategy.INSTANCE);
|
||||
}
|
||||
|
||||
private void testSSLTrustVerificationOverride(final TrustStrategy trustStrategy)
|
||||
throws Exception, IOException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
|
||||
private void testSSLTrustVerificationOverride(final TrustStrategy trustStrategy)
|
||||
throws Exception, IOException, NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
|
||||
// @formatter:off
|
||||
this.server = ServerBootstrap.bootstrap()
|
||||
.setSslContext(SSLTestContexts.createServerSSLContext())
|
||||
.create();
|
||||
// @formatter:on
|
||||
this.server.start();
|
||||
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
|
||||
// @formatter:off
|
||||
final SSLContext sslcontext = SSLContexts.custom()
|
||||
.loadTrustMaterial(null, trustStrategy)
|
||||
.build();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
sslcontext,
|
||||
// @formatter:on
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslcontext,
|
||||
NoopHostnameVerifier.INSTANCE);
|
||||
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
// empty for now
|
||||
}
|
||||
}
|
||||
}
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
// empty for now
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTLSOnly() throws Exception {
|
||||
// @formatter:off
|
||||
this.server = ServerBootstrap.bootstrap()
|
||||
.setSslContext(SSLTestContexts.createServerSSLContext())
|
||||
.setSslSetupHandler(new SSLServerSetupHandler() {
|
||||
|
@ -276,24 +289,26 @@ public class TestSSLSocketFactory {
|
|||
|
||||
})
|
||||
.create();
|
||||
// @formatter:on
|
||||
this.server.start();
|
||||
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext());
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
Assert.assertNotNull(sslsession);
|
||||
}
|
||||
}
|
||||
}
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
|
||||
null, context)) {
|
||||
final SSLSession sslsession = sslSocket.getSession();
|
||||
Assert.assertNotNull(sslsession);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected=IOException.class)
|
||||
@Test(expected = IOException.class)
|
||||
public void testSSLDisabledByDefault() throws Exception {
|
||||
// @formatter:off
|
||||
this.server = ServerBootstrap.bootstrap()
|
||||
.setSslContext(SSLTestContexts.createServerSSLContext())
|
||||
.setSslSetupHandler(new SSLServerSetupHandler() {
|
||||
|
@ -305,15 +320,16 @@ public class TestSSLSocketFactory {
|
|||
|
||||
})
|
||||
.create();
|
||||
// @formatter:on
|
||||
this.server.start();
|
||||
|
||||
final HttpContext context = new BasicHttpContext();
|
||||
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
|
||||
SSLTestContexts.createClientSSLContext());
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
|
||||
}
|
||||
}
|
||||
try (final Socket socket = socketFactory.createSocket(context)) {
|
||||
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
|
||||
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
|
||||
socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue