aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-application/src/test/java/org/sonar/application/ConnectorsTest.java
blob: 4d8e9b4773c5c2c7cfc52141cd94820de061e685 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2013 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
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.net.InetAddress;
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.*;

public class ConnectorsTest {

  Tomcat tomcat = mock(Tomcat.class, Mockito.RETURNS_DEEP_STUBS);

  //---- connectors

  @Test
  public void configure_thread_pool() throws Exception {
    Properties p = new Properties();
    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);

    Connectors.configure(tomcat, props);

    verify(tomcat).setConnector(argThat(new PropertiesMatcher(
      ImmutableMap.<String, Object>of("minSpareThreads", 2, "maxThreads", 30, "acceptCount", 20)
    )));
  }

  @Test
  public void configure_default_thread_pool() throws Exception {
    Props props = new Props(new Properties());

    Connectors.configure(tomcat, props);

    verify(tomcat).setConnector(argThat(new PropertiesMatcher(
      ImmutableMap.<String, Object>of("minSpareThreads", 5, "maxThreads", 50, "acceptCount", 25)
    )));
  }

  @Test
  public void different_thread_pools_for_connectors() throws Exception {
    Properties p = new Properties();
    p.setProperty("sonar.web.port", "9000");
    p.setProperty("sonar.web.http.minThreads", "2");
    p.setProperty("sonar.web.https.port", "9443");
    p.setProperty("sonar.web.https.minThreads", "5");
    Props props = new Props(p);

    Connectors.configure(tomcat, props);

    verify(tomcat.getService()).addConnector(argThat(new ArgumentMatcher<Connector>() {
      @Override
      public boolean matches(Object o) {
        Connector c = (Connector) o;
        return c.getPort() == 9000 && c.getProperty("minSpareThreads").equals(2);
      }
    }));
    verify(tomcat.getService()).addConnector(argThat(new ArgumentMatcher<Connector>() {
      @Override
      public boolean matches(Object o) {
        Connector c = (Connector) o;
        return c.getPort() == 9443 && c.getProperty("minSpareThreads").equals(5);
      }
    }));
  }

  @Test
  public void fail_if_http_connectors_are_disabled() {
    Properties p = new Properties();
    p.setProperty("sonar.web.port", "-1");
    p.setProperty("sonar.web.https.port", "-1");
    Props props = new Props(p);

    try {
      Connectors.configure(tomcat, props);
      fail();
    } catch (IllegalStateException e) {
      assertThat(e.getMessage()).isEqualTo("HTTP connectors are disabled");
    }
  }

  @Test
  public void only_https_is_enabled() {
    Properties p = new Properties();
    p.setProperty("sonar.web.port", "-1");
    p.setProperty("sonar.web.https.port", "9443");
    Props props = new Props(p);

    Connectors.configure(tomcat, props);

    verify(tomcat).setConnector(argThat(new ArgumentMatcher<Connector>() {
      @Override
      public boolean matches(Object o) {
        Connector c = (Connector) o;
        return c.getScheme().equals("https") && c.getPort() == 9443;
      }
    }));
  }

  @Test
  public void all_connectors_are_enabled() {
    Properties p = new Properties();
    p.setProperty("sonar.web.port", "9000");
    p.setProperty("sonar.web.https.port", "9443");
    Props props = new Props(p);

    Connectors.configure(tomcat, props);

    verify(tomcat.getService()).addConnector(argThat(new ArgumentMatcher<Connector>() {
      @Override
      public boolean matches(Object o) {
        Connector c = (Connector) o;
        return c.getScheme().equals("http") && c.getPort() == 9000 && c.getProtocol().equals(Connectors.HTTP_PROTOCOL);
      }
    }));
    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 && c.getProtocol().equals(Connectors.HTTP_PROTOCOL);
      }
    }));
  }

  @Test
  public void http_and_https_ports_should_be_different() throws Exception {
    Properties p = new Properties();
    p.setProperty("sonar.web.port", "9000");
    p.setProperty("sonar.web.https.port", "9000");

    try {
      Connectors.configure(tomcat, new Props(p));
      fail();
    } catch (IllegalStateException e) {
      assertThat(e).hasMessage("HTTP and HTTPS must not use the same port 9000");
    }
  }

  @Test
  public void bind_to_all_addresses_by_default() throws Exception {
    Properties p = new Properties();
    p.setProperty("sonar.web.port", "9000");
    p.setProperty("sonar.web.https.port", "9443");

    Connectors.configure(tomcat, new Props(p));

    verify(tomcat.getService()).addConnector(argThat(new ArgumentMatcher<Connector>() {
      @Override
      public boolean matches(Object o) {
        Connector c = (Connector) o;
        return c.getScheme().equals("http") && c.getPort() == 9000 && ((InetAddress)c.getProperty("address")).getHostAddress().equals("0.0.0.0");
      }
    }));
    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 && ((InetAddress)c.getProperty("address")).getHostAddress().equals("0.0.0.0");
      }
    }));
  }

  @Test
  public void bind_to_specific_address() throws Exception {
    Properties p = new Properties();
    p.setProperty("sonar.web.port", "9000");
    p.setProperty("sonar.web.https.port", "9443");
    p.setProperty("sonar.web.host", "1.2.3.4");

    Connectors.configure(tomcat, new Props(p));

    verify(tomcat.getService()).addConnector(argThat(new ArgumentMatcher<Connector>() {
      @Override
      public boolean matches(Object o) {
        Connector c = (Connector) o;
        return c.getScheme().equals("http") && c.getPort() == 9000 && ((InetAddress)c.getProperty("address")).getHostAddress().equals("1.2.3.4");
      }
    }));
    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 && ((InetAddress)c.getProperty("address")).getHostAddress().equals("1.2.3.4");
      }
    }));
  }


  //---- 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;
    }
  }
}