Use try-with-resources to try and avoid any leaks.

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1783811 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2017-02-20 22:56:52 +00:00
parent 97f1e35a00
commit a9df4780c1
1 changed files with 75 additions and 57 deletions

View File

@ -98,15 +98,17 @@ public class TestSSLSocketFactory {
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
SSLTestContexts.createClientSSLContext(), hostVerifier); SSLTestContexts.createClientSSLContext(), hostVerifier);
final Socket socket = socketFactory.createSocket(context); try (final Socket socket = socketFactory.createSocket(context)) {
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context)) { try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
final SSLSession sslsession = sslSocket.getSession(); context)) {
final SSLSession sslsession = sslSocket.getSession();
Assert.assertNotNull(sslsession); Assert.assertNotNull(sslsession);
Assert.assertTrue(hostVerifier.isFired()); Assert.assertTrue(hostVerifier.isFired());
} }
}
} }
@Test @Test
@ -119,15 +121,17 @@ public class TestSSLSocketFactory {
final HttpContext context = new BasicHttpContext(); final HttpContext context = new BasicHttpContext();
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
SSLTestContexts.createClientSSLContext(), SSLConnectionSocketFactory.getDefaultHostnameVerifier()); SSLTestContexts.createClientSSLContext(), SSLConnectionSocketFactory.getDefaultHostnameVerifier());
final Socket socket = socketFactory.createSocket(context); try (final Socket socket = socketFactory.createSocket(context)) {
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context)) { try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
final SSLSession sslsession = sslSocket.getSession(); context)) {
final SSLSession sslsession = sslSocket.getSession();
Assert.assertNotNull(sslsession); Assert.assertNotNull(sslsession);
} }
} }
}
@Test @Test
public void testClientAuthSSL() throws Exception { public void testClientAuthSSL() throws Exception {
@ -140,16 +144,18 @@ public class TestSSLSocketFactory {
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
SSLTestContexts.createClientSSLContext(), hostVerifier); SSLTestContexts.createClientSSLContext(), hostVerifier);
final Socket socket = socketFactory.createSocket(context); try (final Socket socket = socketFactory.createSocket(context)) {
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context)) { try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
final SSLSession sslsession = sslSocket.getSession(); context)) {
final SSLSession sslsession = sslSocket.getSession();
Assert.assertNotNull(sslsession); Assert.assertNotNull(sslsession);
Assert.assertTrue(hostVerifier.isFired()); Assert.assertTrue(hostVerifier.isFired());
} }
} }
}
@Test(expected=IOException.class) @Test(expected=IOException.class)
public void testClientAuthSSLFailure() throws Exception { public void testClientAuthSSLFailure() throws Exception {
@ -170,16 +176,18 @@ public class TestSSLSocketFactory {
final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier(); final TestX509HostnameVerifier hostVerifier = new TestX509HostnameVerifier();
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
SSLTestContexts.createClientSSLContext(), hostVerifier); SSLTestContexts.createClientSSLContext(), hostVerifier);
final Socket socket = socketFactory.createSocket(context); try (final Socket socket = socketFactory.createSocket(context)) {
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context)) { try (SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null,
final SSLSession sslsession = sslSocket.getSession(); context)) {
final SSLSession sslsession = sslSocket.getSession();
Assert.assertNotNull(sslsession); Assert.assertNotNull(sslsession);
Assert.assertTrue(hostVerifier.isFired()); Assert.assertTrue(hostVerifier.isFired());
} }
} }
}
@Test(expected=SSLException.class) @Test(expected=SSLException.class)
public void testSSLTrustVerification() throws Exception { public void testSSLTrustVerification() throws Exception {
@ -195,12 +203,15 @@ public class TestSSLSocketFactory {
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(defaultsslcontext, final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(defaultsslcontext,
NoopHostnameVerifier.INSTANCE); NoopHostnameVerifier.INSTANCE);
final Socket socket = socketFactory.createSocket(context); try (final Socket socket = socketFactory.createSocket(context)) {
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
sslSocket.close(); null, context)) {
} // empty for now
}
}
}
@Test @Test
public void testSSLTrustVerificationOverrideWithCustsom() throws Exception { public void testSSLTrustVerificationOverrideWithCustsom() throws Exception {
@ -242,11 +253,14 @@ public class TestSSLSocketFactory {
sslcontext, sslcontext,
NoopHostnameVerifier.INSTANCE); NoopHostnameVerifier.INSTANCE);
final Socket socket = socketFactory.createSocket(context); try (final Socket socket = socketFactory.createSocket(context)) {
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
sslSocket.close(); null, context)) {
// empty for now
}
}
} }
@Test @Test
@ -267,13 +281,16 @@ public class TestSSLSocketFactory {
final HttpContext context = new BasicHttpContext(); final HttpContext context = new BasicHttpContext();
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
SSLTestContexts.createClientSSLContext()); SSLTestContexts.createClientSSLContext());
final Socket socket = socketFactory.createSocket(context); try (final Socket socket = socketFactory.createSocket(context)) {
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); try (final SSLSocket sslSocket = (SSLSocket) socketFactory.connectSocket(0, socket, target, remoteAddress,
final SSLSession sslsession = sslSocket.getSession(); null, context)) {
Assert.assertNotNull(sslsession); final SSLSession sslsession = sslSocket.getSession();
} Assert.assertNotNull(sslsession);
}
}
}
@Test(expected=IOException.class) @Test(expected=IOException.class)
public void testSSLDisabledByDefault() throws Exception { public void testSSLDisabledByDefault() throws Exception {
@ -293,9 +310,10 @@ public class TestSSLSocketFactory {
final HttpContext context = new BasicHttpContext(); final HttpContext context = new BasicHttpContext();
final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
SSLTestContexts.createClientSSLContext()); SSLTestContexts.createClientSSLContext());
final Socket socket = socketFactory.createSocket(context); try (final Socket socket = socketFactory.createSocket(context)) {
final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort()); final InetSocketAddress remoteAddress = new InetSocketAddress("localhost", this.server.getLocalPort());
final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https"); final HttpHost target = new HttpHost("localhost", this.server.getLocalPort(), "https");
socketFactory.connectSocket(0, socket, target, remoteAddress, null, context); socketFactory.connectSocket(0, socket, target, remoteAddress, null, context);
} }
}
} }