This commit is contained in:
Phillip Webb 2015-01-05 17:46:33 -08:00
parent dcc21616a9
commit d4b75edfbf
1 changed files with 50 additions and 112 deletions

View File

@ -274,10 +274,8 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
@Test @Test
public void documentRoot() throws Exception { public void documentRoot() throws Exception {
FileCopyUtils.copy("test",
new FileWriter(this.temporaryFolder.newFile("test.txt")));
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setDocumentRoot(this.temporaryFolder.getRoot()); addTestTxtFile(factory);
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
assertThat(getResponse(getLocalUrl("/test.txt")), equalTo("test")); assertThat(getResponse(getLocalUrl("/test.txt")), equalTo("test"));
@ -316,71 +314,41 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
} }
protected final void testBasicSslWithKeyStore(String keyStore) throws Exception { protected final void testBasicSslWithKeyStore(String keyStore) throws Exception {
FileCopyUtils.copy("test",
new FileWriter(this.temporaryFolder.newFile("test.txt")));
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setDocumentRoot(this.temporaryFolder.getRoot()); addTestTxtFile(factory);
factory.setSsl(getSsl(null, "password", keyStore));
Ssl ssl = new Ssl();
ssl.setKeyStore(keyStore);
ssl.setKeyStorePassword("secret");
ssl.setKeyPassword("password");
factory.setSsl(ssl);
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new SSLContextBuilder().loadTrustMaterial(null,
new TrustSelfSignedStrategy()).build()); new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build(); .build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient); httpClient);
assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory),
equalTo("test")); equalTo("test"));
} }
@Test @Test
public void pkcs12KeyStoreAndTrustStore() throws Exception { public void pkcs12KeyStoreAndTrustStore() throws Exception {
FileCopyUtils.copy("test",
new FileWriter(this.temporaryFolder.newFile("test.txt")));
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setDocumentRoot(this.temporaryFolder.getRoot()); addTestTxtFile(factory);
factory.setSsl(getSsl(ClientAuth.NEED, null, "src/test/resources/test.p12",
Ssl ssl = new Ssl(); "src/test/resources/test.p12"));
ssl.setKeyStore("src/test/resources/test.p12");
ssl.setKeyStorePassword("secret");
ssl.setKeyStoreType("pkcs12");
ssl.setTrustStore("src/test/resources/test.p12");
ssl.setTrustStorePassword("secret");
ssl.setTrustStoreType("pkcs12");
ssl.setClientAuth(ClientAuth.NEED);
factory.setSsl(ssl);
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
KeyStore keyStore = KeyStore.getInstance("pkcs12"); KeyStore keyStore = KeyStore.getInstance("pkcs12");
keyStore.load(new FileInputStream(new File("src/test/resources/test.p12")), keyStore.load(new FileInputStream(new File("src/test/resources/test.p12")),
"secret".toCharArray()); "secret".toCharArray());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder() new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "secret".toCharArray()).build()); .loadKeyMaterial(keyStore, "secret".toCharArray()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build(); .build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient); httpClient);
assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory),
equalTo("test")); equalTo("test"));
} }
@ -388,39 +356,22 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
@Test @Test
public void sslNeedsClientAuthenticationSucceedsWithClientCertificate() public void sslNeedsClientAuthenticationSucceedsWithClientCertificate()
throws Exception { throws Exception {
FileCopyUtils.copy("test",
new FileWriter(this.temporaryFolder.newFile("test.txt")));
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setDocumentRoot(this.temporaryFolder.getRoot()); addTestTxtFile(factory);
factory.setSsl(getSsl(ClientAuth.NEED, "password", "src/test/resources/test.jks"));
Ssl ssl = new Ssl();
ssl.setKeyStore("src/test/resources/test.jks");
ssl.setKeyStorePassword("secret");
ssl.setKeyPassword("password");
ssl.setClientAuth(ClientAuth.NEED);
ssl.setTrustStore("src/test/resources/test.jks");
ssl.setTrustStorePassword("secret");
factory.setSsl(ssl);
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")),
"secret".toCharArray()); "secret".toCharArray());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder() new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray()).build()); .loadKeyMaterial(keyStore, "password".toCharArray()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build(); .build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient); httpClient);
assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory),
equalTo("test")); equalTo("test"));
} }
@ -428,73 +379,40 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
@Test(expected = IOException.class) @Test(expected = IOException.class)
public void sslNeedsClientAuthenticationFailsWithoutClientCertificate() public void sslNeedsClientAuthenticationFailsWithoutClientCertificate()
throws Exception { throws Exception {
FileCopyUtils.copy("test",
new FileWriter(this.temporaryFolder.newFile("test.txt")));
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setDocumentRoot(this.temporaryFolder.getRoot()); addTestTxtFile(factory);
factory.setSsl(getSsl(ClientAuth.NEED, "password", "src/test/resources/test.jks"));
Ssl ssl = new Ssl();
ssl.setKeyStore("src/test/resources/test.jks");
ssl.setKeyStorePassword("secret");
ssl.setKeyPassword("password");
ssl.setClientAuth(ClientAuth.NEED);
ssl.setTrustStore("src/test/resources/test.jks");
ssl.setTrustStorePassword("secret");
factory.setSsl(ssl);
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new SSLContextBuilder().loadTrustMaterial(null,
new TrustSelfSignedStrategy()).build()); new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build(); .build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient); httpClient);
getResponse(getLocalUrl("https", "/test.txt"), requestFactory); getResponse(getLocalUrl("https", "/test.txt"), requestFactory);
} }
@Test @Test
public void sslWantsClientAuthenticationSucceedsWithClientCertificate() public void sslWantsClientAuthenticationSucceedsWithClientCertificate()
throws Exception { throws Exception {
FileCopyUtils.copy("test",
new FileWriter(this.temporaryFolder.newFile("test.txt")));
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setDocumentRoot(this.temporaryFolder.getRoot()); addTestTxtFile(factory);
factory.setSsl(getSsl(ClientAuth.WANT, "password", "src/test/resources/test.jks"));
Ssl ssl = new Ssl();
ssl.setKeyStore("src/test/resources/test.jks");
ssl.setKeyStorePassword("secret");
ssl.setKeyPassword("password");
ssl.setClientAuth(ClientAuth.WANT);
ssl.setTrustStore("src/test/resources/test.jks");
ssl.setTrustStorePassword("secret");
factory.setSsl(ssl);
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")),
"secret".toCharArray()); "secret".toCharArray());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder() new SSLContextBuilder()
.loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadTrustMaterial(null, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray()).build()); .loadKeyMaterial(keyStore, "password".toCharArray()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build(); .build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient); httpClient);
assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory),
equalTo("test")); equalTo("test"));
} }
@ -502,43 +420,62 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
@Test @Test
public void sslWantsClientAuthenticationSucceedsWithoutClientCertificate() public void sslWantsClientAuthenticationSucceedsWithoutClientCertificate()
throws Exception { throws Exception {
FileCopyUtils.copy("test",
new FileWriter(this.temporaryFolder.newFile("test.txt")));
AbstractEmbeddedServletContainerFactory factory = getFactory(); AbstractEmbeddedServletContainerFactory factory = getFactory();
factory.setDocumentRoot(this.temporaryFolder.getRoot()); addTestTxtFile(factory);
factory.setSsl(getSsl(ClientAuth.WANT, "password", "src/test/resources/test.jks"));
Ssl ssl = new Ssl();
ssl.setKeyStore("src/test/resources/test.jks");
ssl.setKeyStorePassword("secret");
ssl.setKeyPassword("password");
ssl.setClientAuth(ClientAuth.WANT);
ssl.setTrustStore("src/test/resources/test.jks");
ssl.setTrustStorePassword("secret");
factory.setSsl(ssl);
this.container = factory.getEmbeddedServletContainer(); this.container = factory.getEmbeddedServletContainer();
this.container.start(); this.container.start();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
new SSLContextBuilder().loadTrustMaterial(null, new SSLContextBuilder().loadTrustMaterial(null,
new TrustSelfSignedStrategy()).build()); new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory)
.build(); .build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(
httpClient); httpClient);
assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory),
equalTo("test")); equalTo("test"));
} }
private Ssl getSsl(ClientAuth clientAuth, String keyPassword, String keyStore) {
return getSsl(clientAuth, keyPassword, keyStore, null);
}
private Ssl getSsl(ClientAuth clientAuth, String keyPassword, String keyStore,
String trustStore) {
Ssl ssl = new Ssl();
ssl.setClientAuth(clientAuth);
if (keyPassword != null) {
ssl.setKeyPassword(keyPassword);
}
if (keyStore != null) {
ssl.setKeyStore(keyStore);
ssl.setKeyStorePassword("secret");
ssl.setKeyStoreType(getStoreType(keyStore));
}
if (trustStore != null) {
ssl.setTrustStore(trustStore);
ssl.setTrustStorePassword("secret");
ssl.setTrustStoreType(getStoreType(trustStore));
}
return ssl;
}
private String getStoreType(String keyStore) {
return (keyStore.endsWith(".p12") ? "pkcs12" : null);
}
@Test @Test
public void defaultSessionTimeout() throws Exception { public void defaultSessionTimeout() throws Exception {
assertThat(getFactory().getSessionTimeout(), equalTo(30 * 60)); assertThat(getFactory().getSessionTimeout(), equalTo(30 * 60));
} }
private void addTestTxtFile(AbstractEmbeddedServletContainerFactory factory)
throws IOException {
FileCopyUtils.copy("test",
new FileWriter(this.temporaryFolder.newFile("test.txt")));
factory.setDocumentRoot(this.temporaryFolder.getRoot());
}
protected String getLocalUrl(String resourcePath) { protected String getLocalUrl(String resourcePath) {
return getLocalUrl("http", resourcePath); return getLocalUrl("http", resourcePath);
} }
@ -625,4 +562,5 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests {
return this.initCount; return this.initCount;
} }
}; };
} }