diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-14 01:00:12 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-14 01:00:12 +0200 |
commit | 275da1c743917341256322a09a3172a2ccf04ef3 (patch) | |
tree | 42f62b18140966f7da89847f6033ce8691700285 /sonar-application/src/test/java/org/sonar | |
parent | 5b4b4172c45aa794d7b40c8e5b9fde6d3a001492 (diff) | |
download | sonarqube-275da1c743917341256322a09a3172a2ccf04ef3.tar.gz sonarqube-275da1c743917341256322a09a3172a2ccf04ef3.zip |
SONAR-4741 HTTPS mode
Diffstat (limited to 'sonar-application/src/test/java/org/sonar')
4 files changed, 156 insertions, 37 deletions
diff --git a/sonar-application/src/test/java/org/sonar/application/ConnectorsTest.java b/sonar-application/src/test/java/org/sonar/application/ConnectorsTest.java index b40b79b32f0..c1594b2c46a 100644 --- a/sonar-application/src/test/java/org/sonar/application/ConnectorsTest.java +++ b/sonar-application/src/test/java/org/sonar/application/ConnectorsTest.java @@ -19,98 +19,167 @@ */ package org.sonar.application; +import com.google.common.collect.ImmutableMap; import org.apache.catalina.connector.Connector; import org.apache.catalina.startup.Tomcat; import org.junit.Test; import org.mockito.ArgumentMatcher; import org.mockito.Mockito; +import java.util.Map; import java.util.Properties; +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; public class ConnectorsTest { + + Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS); + + //---- connectors + @Test - public void enable_shutdown_port() throws Exception { + public void configure_thread_pool() throws Exception { Properties p = new Properties(); - p.setProperty(Connectors.PROPERTY_SHUTDOWN_PORT, "9010"); - p.setProperty(Connectors.PROPERTY_SHUTDOWN_TOKEN, "SHUTDOWN"); + p.setProperty("sonar.web.http.minThreads", "2"); + p.setProperty("sonar.web.http.maxThreads", "30"); + p.setProperty("sonar.web.http.acceptCount", "20"); Props props = new Props(p); - Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS); Connectors.configure(tomcat, props); - verify(tomcat.getServer()).setPort(9010); - verify(tomcat.getServer()).setShutdown("SHUTDOWN"); + verify(tomcat).setConnector(argThat(new PropertiesMatcher( + ImmutableMap.<String, Object>of("minSpareThreads", 2, "maxThreads", 30, "acceptCount", 20) + ))); } @Test - public void disable_shutdown_port_by_default() throws Exception { + public void configure_default_thread_pool() throws Exception { Props props = new Props(new Properties()); - Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS); Connectors.configure(tomcat, props); - verify(tomcat.getServer(), never()).setPort(anyInt()); - verify(tomcat.getServer(), never()).setShutdown(anyString()); + verify(tomcat).setConnector(argThat(new PropertiesMatcher( + ImmutableMap.<String, Object>of("minSpareThreads", 5, "maxThreads", 50, "acceptCount", 25) + ))); } @Test - public void disable_shutdown_port_if_missing_token() throws Exception { + public void fail_if_both_http_and_https_are_disabled() { Properties p = new Properties(); - // only the port, but not the token - p.setProperty(Connectors.PROPERTY_SHUTDOWN_PORT, "9010"); + p.setProperty("sonar.web.http.enable", "false"); + p.setProperty("sonar.web.https.enable", "false"); Props props = new Props(p); - Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS); - Connectors.configure(tomcat, props); - - verify(tomcat.getServer(), never()).setPort(anyInt()); - verify(tomcat.getServer(), never()).setShutdown(anyString()); + try { + Connectors.configure(tomcat, props); + fail(); + } catch (IllegalStateException e) { + assertThat(e.getMessage()).isEqualTo("Both HTTP and HTTPS connectors are disabled"); + } } @Test - public void configure_thread_pool() throws Exception { + public void only_https_is_enabled() { Properties p = new Properties(); - p.setProperty(Connectors.PROPERTY_MIN_THREADS, "2"); - p.setProperty(Connectors.PROPERTY_MAX_THREADS, "30"); - p.setProperty(Connectors.PROPERTY_ACCEPT_COUNT, "20"); + p.setProperty("sonar.web.http.enable", "false"); + p.setProperty("sonar.web.https.enable", "true"); Props props = new Props(p); - Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS); Connectors.configure(tomcat, props); verify(tomcat).setConnector(argThat(new ArgumentMatcher<Connector>() { @Override public boolean matches(Object o) { Connector c = (Connector)o; - return (Integer)c.getProperty("minSpareThreads") == 2 && - (Integer) c.getProperty("maxThreads") == 30 && - (Integer) c.getProperty("acceptCount") == 20; + return c.getScheme().equals("https") && c.getPort()==9443; } })); } @Test - public void configure_default_thread_pool() throws Exception { - Props props = new Props(new Properties()); + public void both_http_and_https_are_enabled() { + Properties p = new Properties(); + p.setProperty("sonar.web.http.enable", "true"); + p.setProperty("sonar.web.https.enable", "true"); + Props props = new Props(p); - Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS); Connectors.configure(tomcat, props); - verify(tomcat).setConnector(argThat(new ArgumentMatcher<Connector>() { + verify(tomcat.getService()).addConnector(argThat(new ArgumentMatcher<Connector>() { @Override public boolean matches(Object o) { Connector c = (Connector)o; - return (Integer)c.getProperty("minSpareThreads") == 5 && - (Integer) c.getProperty("maxThreads") == 50 && - (Integer) c.getProperty("acceptCount") == 25; + return c.getScheme().equals("http") && c.getPort()==9000; } })); + verify(tomcat.getService()).addConnector(argThat(new ArgumentMatcher<Connector>() { + @Override + public boolean matches(Object o) { + Connector c = (Connector)o; + return c.getScheme().equals("https") && c.getPort()==9443; + } + })); + } + + + //---- shutdown port + + @Test + public void enable_shutdown_port() throws Exception { + Properties p = new Properties(); + p.setProperty("sonar.web.shutdown.port", "9010"); + p.setProperty("sonar.web.shutdown.token", "SHUTDOWN"); + Props props = new Props(p); + + Connectors.configure(tomcat, props); + + verify(tomcat.getServer()).setPort(9010); + verify(tomcat.getServer()).setShutdown("SHUTDOWN"); + } + + @Test + public void disable_shutdown_port_by_default() throws Exception { + Props props = new Props(new Properties()); + + Connectors.configure(tomcat, props); + + verify(tomcat.getServer(), never()).setPort(anyInt()); + verify(tomcat.getServer(), never()).setShutdown(anyString()); + } + + @Test + public void disable_shutdown_port_if_missing_token() throws Exception { + Properties p = new Properties(); + // only the port, but not the token + p.setProperty("sonar.web.shutdown.port", "9010"); + Props props = new Props(p); + + Connectors.configure(tomcat, props); + + verify(tomcat.getServer(), never()).setPort(anyInt()); + verify(tomcat.getServer(), never()).setShutdown(anyString()); + } + + private static class PropertiesMatcher extends ArgumentMatcher<Connector> { + private final Map<String, Object> expected; + + PropertiesMatcher(Map<String, Object> expected) { + this.expected = expected; + } + + public boolean matches(Object o) { + Connector c = (Connector) o; + for (Map.Entry<String, Object> entry : expected.entrySet()) { + if (!entry.getValue().equals(c.getProperty(entry.getKey()))) { + return false; + } + } + return true; + } } } diff --git a/sonar-application/src/test/java/org/sonar/application/LoggingTest.java b/sonar-application/src/test/java/org/sonar/application/LoggingTest.java index ca5f6ef3c51..39808394b88 100644 --- a/sonar-application/src/test/java/org/sonar/application/LoggingTest.java +++ b/sonar-application/src/test/java/org/sonar/application/LoggingTest.java @@ -20,11 +20,14 @@ package org.sonar.application; import ch.qos.logback.access.tomcat.LogbackValve; +import org.apache.catalina.Lifecycle; +import org.apache.catalina.LifecycleEvent; import org.apache.catalina.Valve; import org.apache.catalina.startup.Tomcat; import org.junit.Test; import org.mockito.ArgumentMatcher; import org.mockito.Mockito; +import org.slf4j.Logger; import java.io.File; @@ -65,4 +68,18 @@ public class LoggingTest { assertThat(e).hasMessage("File is missing: " + confFile.getAbsolutePath()); } } + + @Test + public void log_when_started() { + Logger logger = mock(Logger.class); + Logging.StartupLogger listener = new Logging.StartupLogger(logger); + + LifecycleEvent event = new LifecycleEvent(mock(Lifecycle.class), "before_init", null); + listener.lifecycleEvent(event); + verifyZeroInteractions(logger); + + event = new LifecycleEvent(mock(Lifecycle.class), "after_start", null); + listener.lifecycleEvent(event); + verify(logger).info("Web server is up"); + } } diff --git a/sonar-application/src/test/java/org/sonar/application/PropsTest.java b/sonar-application/src/test/java/org/sonar/application/PropsTest.java index f6c1f2ece01..20ca51e5161 100644 --- a/sonar-application/src/test/java/org/sonar/application/PropsTest.java +++ b/sonar-application/src/test/java/org/sonar/application/PropsTest.java @@ -85,6 +85,19 @@ public class PropsTest { } @Test + public void booleanOf_default_value() throws Exception { + Properties p = new Properties(); + p.setProperty("foo", "true"); + p.setProperty("bar", "false"); + Props props = new Props(p); + + assertThat(props.booleanOf("unset", false)).isFalse(); + assertThat(props.booleanOf("unset", true)).isTrue(); + assertThat(props.booleanOf("foo", false)).isTrue(); + assertThat(props.booleanOf("bar", true)).isFalse(); + } + + @Test public void load_file_and_system_properties() throws Exception { Env env = mock(Env.class); File propsFile = new File(getClass().getResource("/org/sonar/application/PropsTest/sonar.properties").toURI()); diff --git a/sonar-application/src/test/java/org/sonar/application/WebappTest.java b/sonar-application/src/test/java/org/sonar/application/WebappTest.java index 0e0dd904a1c..aa1ba82eee8 100644 --- a/sonar-application/src/test/java/org/sonar/application/WebappTest.java +++ b/sonar-application/src/test/java/org/sonar/application/WebappTest.java @@ -26,6 +26,7 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; import java.io.File; +import java.util.Properties; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; @@ -46,7 +47,7 @@ public class WebappTest { when(tomcat.addContext("", webDir.getAbsolutePath())).thenThrow(new NullPointerException()); try { - Webapp.configure(tomcat, env, mock(Props.class)); + Webapp.configure(tomcat, env, new Props(new Properties())); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Fail to configure webapp"); @@ -76,4 +77,23 @@ public class WebappTest { verify(context).addParameter("jruby.max.runtimes", "1"); verify(context).addParameter("rails.env", "production"); } + + @Test + public void context_must_start_with_slash() throws Exception { + Properties p = new Properties(); + p.setProperty("sonar.web.context", "foo"); + + try { + Webapp.getContext(new Props(p)); + fail(); + } catch (IllegalStateException e) { + assertThat(e.getMessage()).isEqualTo("Value of sonar.web.context must start with a forward slash: foo"); + } + } + + @Test + public void default_context_is_root() throws Exception { + String context = Webapp.getContext(new Props(new Properties())); + assertThat(context).isEqualTo(""); + } } |