/
ConfigurationTest.java
223 lines (168 loc) · 9.52 KB
/
ConfigurationTest.java
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
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2;
import com.github.joschi.jadconfig.JadConfig;
import com.github.joschi.jadconfig.ParameterException;
import com.github.joschi.jadconfig.RepositoryException;
import com.github.joschi.jadconfig.ValidationException;
import com.github.joschi.jadconfig.repositories.InMemoryRepository;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class ConfigurationTest {
@Rule
public final ExpectedException expectedException = ExpectedException.none();
private Map<String, String> validProperties;
@Before
public void setUp() throws Exception {
validProperties = new HashMap<>();
// Required properties
validProperties.put("password_secret", "ipNUnWxmBLCxTEzXcyamrdy0Q3G7HxdKsAvyg30R9SCof0JydiZFiA3dLSkRsbLF");
// SHA-256 of "admin"
validProperties.put("root_password_sha2", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918");
}
@Test
public void testRestListenUriIsRelativeURI() throws RepositoryException, ValidationException {
validProperties.put("rest_listen_uri", "/foo");
expectedException.expect(ValidationException.class);
expectedException.expectMessage("Parameter rest_listen_uri should be an absolute URI (found /foo)");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
}
@Test
public void testWebListenUriIsRelativeURI() throws RepositoryException, ValidationException {
validProperties.put("web_listen_uri", "/foo");
expectedException.expect(ValidationException.class);
expectedException.expectMessage("Parameter web_listen_uri should be an absolute URI (found /foo)");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
}
@Test
public void testRestListenUriIsAbsoluteURI() throws RepositoryException, ValidationException {
validProperties.put("rest_listen_uri", "http://www.example.com:12900/");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getRestListenUri()).isEqualTo(URI.create("http://www.example.com:12900/"));
}
@Test
public void testWebListenUriIsAbsoluteURI() throws RepositoryException, ValidationException {
validProperties.put("web_listen_uri", "http://www.example.com:12900/web");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getWebListenUri()).isEqualTo(URI.create("http://www.example.com:12900/web/"));
}
@Test
public void testRestListenUriWithHttpDefaultPort() throws RepositoryException, ValidationException {
validProperties.put("rest_listen_uri", "http://example.com/");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getRestListenUri()).hasPort(80);
}
@Test
public void testRestListenUriWithCustomPort() throws RepositoryException, ValidationException {
validProperties.put("rest_listen_uri", "http://example.com:12900/");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getRestListenUri()).hasPort(12900);
}
@Test
public void testWebListenUriWithHttpDefaultPort() throws RepositoryException, ValidationException {
validProperties.put("web_listen_uri", "http://example.com/");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getWebListenUri()).hasPort(80);
}
@Test
public void testWebListenUriWithCustomPort() throws RepositoryException, ValidationException {
validProperties.put("web_listen_uri", "http://example.com:9000/");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getWebListenUri()).hasPort(9000);
}
@Test
public void testPasswordSecretIsTooShort() throws ValidationException, RepositoryException {
validProperties.put("password_secret", "too short");
expectedException.expect(ValidationException.class);
expectedException.expectMessage("The minimum length for \"password_secret\" is 16 characters.");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
}
@Test
public void testPasswordSecretIsEmpty() throws ValidationException, RepositoryException {
validProperties.put("password_secret", "");
expectedException.expect(ValidationException.class);
expectedException.expectMessage("Parameter password_secret should not be blank");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
}
@Test
public void testPasswordSecretIsNull() throws ValidationException, RepositoryException {
validProperties.put("password_secret", null);
expectedException.expect(ParameterException.class);
expectedException.expectMessage("Required parameter \"password_secret\" not found.");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
}
@Test
public void testApiListenerOnRootAndWebListenerOnSubPath() throws ValidationException, RepositoryException {
validProperties.put("rest_listen_uri", "http://0.0.0.0:12900/");
validProperties.put("web_listen_uri", "http://0.0.0.0:12900/web/");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getRestListenUri()).isEqualTo(URI.create("http://0.0.0.0:12900/"));
assertThat(configuration.getWebListenUri()).isEqualTo(URI.create("http://0.0.0.0:12900/web/"));
}
@Test
public void testWebListenerOnRootAndApiListenerOnSubPath() throws ValidationException, RepositoryException {
validProperties.put("rest_listen_uri", "http://0.0.0.0:9000/api/");
validProperties.put("web_listen_uri", "http://0.0.0.0:9000/");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getRestListenUri()).isEqualTo(URI.create("http://0.0.0.0:9000/api/"));
assertThat(configuration.getWebListenUri()).isEqualTo(URI.create("http://0.0.0.0:9000/"));
}
@Test
public void testPasswordSecretIsValid() throws ValidationException, RepositoryException {
validProperties.put("password_secret", "abcdefghijklmnopqrstuvwxyz");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
assertThat(configuration.getPasswordSecret()).isEqualTo("abcdefghijklmnopqrstuvwxyz");
}
@Test
public void testRestApiListeningOnWildcardOnSamePortAsWebInterface() throws ValidationException, RepositoryException {
validProperties.put("rest_listen_uri", "http://0.0.0.0:9000/api/");
validProperties.put("web_listen_uri", "http://127.0.0.1:9000/");
expectedException.expect(ValidationException.class);
expectedException.expectMessage("Wildcard IP addresses cannot be used if the Graylog REST API and web interface listen on the same port.");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
}
@Test
public void testWebInterfaceListeningOnWildcardOnSamePortAsRestApi() throws ValidationException, RepositoryException {
validProperties.put("rest_listen_uri", "http://127.0.0.1:9000/api/");
validProperties.put("web_listen_uri", "http://0.0.0.0:9000/");
expectedException.expect(ValidationException.class);
expectedException.expectMessage("Wildcard IP addresses cannot be used if the Graylog REST API and web interface listen on the same port.");
Configuration configuration = new Configuration();
new JadConfig(new InMemoryRepository(validProperties), configuration).process();
}
}