From d4b75edfbf1c36def40e1b568a4342675337de65 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 5 Jan 2015 17:46:33 -0800 Subject: [PATCH] Polish --- ...tEmbeddedServletContainerFactoryTests.java | 162 ++++++------------ 1 file changed, 50 insertions(+), 112 deletions(-) diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java index 93968448d3..ce96134fe4 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/AbstractEmbeddedServletContainerFactoryTests.java @@ -274,10 +274,8 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { @Test public void documentRoot() throws Exception { - FileCopyUtils.copy("test", - new FileWriter(this.temporaryFolder.newFile("test.txt"))); AbstractEmbeddedServletContainerFactory factory = getFactory(); - factory.setDocumentRoot(this.temporaryFolder.getRoot()); + addTestTxtFile(factory); this.container = factory.getEmbeddedServletContainer(); this.container.start(); assertThat(getResponse(getLocalUrl("/test.txt")), equalTo("test")); @@ -316,71 +314,41 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { } protected final void testBasicSslWithKeyStore(String keyStore) throws Exception { - FileCopyUtils.copy("test", - new FileWriter(this.temporaryFolder.newFile("test.txt"))); - AbstractEmbeddedServletContainerFactory factory = getFactory(); - factory.setDocumentRoot(this.temporaryFolder.getRoot()); - - Ssl ssl = new Ssl(); - ssl.setKeyStore(keyStore); - ssl.setKeyStorePassword("secret"); - ssl.setKeyPassword("password"); - factory.setSsl(ssl); - + addTestTxtFile(factory); + factory.setSsl(getSsl(null, "password", keyStore)); this.container = factory.getEmbeddedServletContainer(); this.container.start(); - SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); - HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); - HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); - assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), equalTo("test")); } @Test public void pkcs12KeyStoreAndTrustStore() throws Exception { - FileCopyUtils.copy("test", - new FileWriter(this.temporaryFolder.newFile("test.txt"))); - AbstractEmbeddedServletContainerFactory factory = getFactory(); - factory.setDocumentRoot(this.temporaryFolder.getRoot()); - - Ssl ssl = new Ssl(); - 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); - + addTestTxtFile(factory); + factory.setSsl(getSsl(ClientAuth.NEED, null, "src/test/resources/test.p12", + "src/test/resources/test.p12")); this.container = factory.getEmbeddedServletContainer(); this.container.start(); - KeyStore keyStore = KeyStore.getInstance("pkcs12"); keyStore.load(new FileInputStream(new File("src/test/resources/test.p12")), "secret".toCharArray()); - SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadKeyMaterial(keyStore, "secret".toCharArray()).build()); - HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); - HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); - assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), equalTo("test")); } @@ -388,39 +356,22 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { @Test public void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exception { - FileCopyUtils.copy("test", - new FileWriter(this.temporaryFolder.newFile("test.txt"))); - AbstractEmbeddedServletContainerFactory factory = getFactory(); - factory.setDocumentRoot(this.temporaryFolder.getRoot()); - - 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); - + addTestTxtFile(factory); + factory.setSsl(getSsl(ClientAuth.NEED, "password", "src/test/resources/test.jks")); this.container = factory.getEmbeddedServletContainer(); this.container.start(); - KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray()); - SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadKeyMaterial(keyStore, "password".toCharArray()).build()); - HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); - HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); - assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), equalTo("test")); } @@ -428,73 +379,40 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { @Test(expected = IOException.class) public void sslNeedsClientAuthenticationFailsWithoutClientCertificate() throws Exception { - FileCopyUtils.copy("test", - new FileWriter(this.temporaryFolder.newFile("test.txt"))); - AbstractEmbeddedServletContainerFactory factory = getFactory(); - factory.setDocumentRoot(this.temporaryFolder.getRoot()); - - 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); - + addTestTxtFile(factory); + factory.setSsl(getSsl(ClientAuth.NEED, "password", "src/test/resources/test.jks")); this.container = factory.getEmbeddedServletContainer(); this.container.start(); - SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); - HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); - HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); - getResponse(getLocalUrl("https", "/test.txt"), requestFactory); } @Test public void sslWantsClientAuthenticationSucceedsWithClientCertificate() throws Exception { - FileCopyUtils.copy("test", - new FileWriter(this.temporaryFolder.newFile("test.txt"))); - AbstractEmbeddedServletContainerFactory factory = getFactory(); - factory.setDocumentRoot(this.temporaryFolder.getRoot()); - - 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); - + addTestTxtFile(factory); + factory.setSsl(getSsl(ClientAuth.WANT, "password", "src/test/resources/test.jks")); this.container = factory.getEmbeddedServletContainer(); this.container.start(); - KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(new FileInputStream(new File("src/test/resources/test.jks")), "secret".toCharArray()); - SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder() .loadTrustMaterial(null, new TrustSelfSignedStrategy()) .loadKeyMaterial(keyStore, "password".toCharArray()).build()); - HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); - HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); - assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), equalTo("test")); } @@ -502,43 +420,62 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { @Test public void sslWantsClientAuthenticationSucceedsWithoutClientCertificate() throws Exception { - FileCopyUtils.copy("test", - new FileWriter(this.temporaryFolder.newFile("test.txt"))); - AbstractEmbeddedServletContainerFactory factory = getFactory(); - factory.setDocumentRoot(this.temporaryFolder.getRoot()); - - 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); - + addTestTxtFile(factory); + factory.setSsl(getSsl(ClientAuth.WANT, "password", "src/test/resources/test.jks")); this.container = factory.getEmbeddedServletContainer(); this.container.start(); - SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory( new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build()); - HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory) .build(); - HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); - assertThat(getResponse(getLocalUrl("https", "/test.txt"), requestFactory), 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 public void defaultSessionTimeout() throws Exception { 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) { return getLocalUrl("http", resourcePath); } @@ -625,4 +562,5 @@ public abstract class AbstractEmbeddedServletContainerFactoryTests { return this.initCount; } }; + }