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 package org.eclipse.jgit.junit.http;
45
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
48
49 import java.io.IOException;
50 import java.net.InetAddress;
51 import java.net.URI;
52 import java.net.URISyntaxException;
53 import java.net.UnknownHostException;
54 import java.util.ArrayList;
55 import java.util.List;
56
57 import org.eclipse.jetty.security.Authenticator;
58 import org.eclipse.jetty.security.ConstraintMapping;
59 import org.eclipse.jetty.security.ConstraintSecurityHandler;
60 import org.eclipse.jetty.security.MappedLoginService;
61 import org.eclipse.jetty.security.authentication.BasicAuthenticator;
62 import org.eclipse.jetty.server.Connector;
63 import org.eclipse.jetty.server.HttpConfiguration;
64 import org.eclipse.jetty.server.HttpConnectionFactory;
65 import org.eclipse.jetty.server.Server;
66 import org.eclipse.jetty.server.ServerConnector;
67 import org.eclipse.jetty.server.UserIdentity;
68 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
69 import org.eclipse.jetty.servlet.ServletContextHandler;
70 import org.eclipse.jetty.util.security.Constraint;
71 import org.eclipse.jetty.util.security.Password;
72 import org.eclipse.jgit.transport.URIish;
73
74
75
76
77
78
79
80
81 public class AppServer {
82
83 public static final String realm = "Secure Area";
84
85
86 public static final String username = "agitter";
87
88
89 public static final String password = "letmein";
90
91 static {
92
93
94 final String prop = "org.eclipse.jetty.util.log.class";
95 System.setProperty(prop, RecordingLogger.class.getName());
96 }
97
98 private final Server server;
99
100 private final ServerConnector connector;
101
102 private final ContextHandlerCollection contexts;
103
104 private final TestRequestLog log;
105
106 public AppServer() {
107 this(0);
108 }
109
110
111
112
113
114
115 public AppServer(int port) {
116 server = new Server();
117
118 HttpConfiguration http_config = new HttpConfiguration();
119 http_config.setSecureScheme("https");
120 http_config.setSecurePort(8443);
121 http_config.setOutputBufferSize(32768);
122
123 connector = new ServerConnector(server,
124 new HttpConnectionFactory(http_config));
125 connector.setPort(port);
126 try {
127 final InetAddress me = InetAddress.getByName("localhost");
128 connector.setHost(me.getHostAddress());
129 } catch (UnknownHostException e) {
130 throw new RuntimeException("Cannot find localhost", e);
131 }
132
133 contexts = new ContextHandlerCollection();
134
135 log = new TestRequestLog();
136 log.setHandler(contexts);
137
138 server.setConnectors(new Connector[] { connector });
139 server.setHandler(log);
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153 public ServletContextHandler addContext(String path) {
154 assertNotYetSetUp();
155 if ("".equals(path))
156 path = "/";
157
158 ServletContextHandler ctx = new ServletContextHandler();
159 ctx.setContextPath(path);
160 contexts.addHandler(ctx);
161
162 return ctx;
163 }
164
165 public ServletContextHandler authBasic(ServletContextHandler ctx) {
166 assertNotYetSetUp();
167 auth(ctx, new BasicAuthenticator());
168 return ctx;
169 }
170
171 static class TestMappedLoginService extends MappedLoginService {
172 private String role;
173
174 TestMappedLoginService(String role) {
175 this.role = role;
176 }
177
178 @Override
179 protected UserIdentity loadUser(String who) {
180 return null;
181 }
182
183 @Override
184 protected void loadUsers() throws IOException {
185 putUser(username, new Password(password), new String[] { role });
186 }
187
188 protected String[] loadRoleInfo(KnownUser user) {
189 return null;
190 }
191
192 protected KnownUser loadUserInfo(String usrname) {
193 return null;
194 }
195 }
196
197 private void auth(ServletContextHandler ctx, Authenticator authType) {
198 final String role = "can-access";
199
200 MappedLoginService users = new TestMappedLoginService(role);
201 ConstraintMapping cm = new ConstraintMapping();
202 cm.setConstraint(new Constraint());
203 cm.getConstraint().setAuthenticate(true);
204 cm.getConstraint().setDataConstraint(Constraint.DC_NONE);
205 cm.getConstraint().setRoles(new String[] { role });
206 cm.setPathSpec("/*");
207
208 ConstraintSecurityHandler sec = new ConstraintSecurityHandler();
209 sec.setRealmName(realm);
210 sec.setAuthenticator(authType);
211 sec.setLoginService(users);
212 sec.setConstraintMappings(new ConstraintMapping[] { cm });
213 sec.setHandler(ctx);
214
215 contexts.removeHandler(ctx);
216 contexts.addHandler(sec);
217 }
218
219
220
221
222
223
224
225 public void setUp() throws Exception {
226 RecordingLogger.clear();
227 log.clear();
228 server.start();
229 }
230
231
232
233
234
235
236
237 public void tearDown() throws Exception {
238 RecordingLogger.clear();
239 log.clear();
240 server.stop();
241 }
242
243
244
245
246
247
248
249
250
251 public URI getURI() {
252 assertAlreadySetUp();
253 String host = connector.getHost();
254 if (host.contains(":") && !host.startsWith("["))
255 host = "[" + host + "]";
256 final String uri = "http://" + host + ":" + getPort();
257 try {
258 return new URI(uri);
259 } catch (URISyntaxException e) {
260 throw new RuntimeException("Unexpected URI error on " + uri, e);
261 }
262 }
263
264
265 public int getPort() {
266 assertAlreadySetUp();
267 return connector.getLocalPort();
268 }
269
270
271 public List<AccessEvent> getRequests() {
272 return new ArrayList<>(log.getEvents());
273 }
274
275
276
277
278
279
280
281
282 public List<AccessEvent> getRequests(URIish base, String path) {
283 return getRequests(HttpTestCase.join(base, path));
284 }
285
286
287
288
289
290
291 public List<AccessEvent> getRequests(String path) {
292 ArrayList<AccessEvent> r = new ArrayList<>();
293 for (AccessEvent event : log.getEvents()) {
294 if (event.getPath().equals(path)) {
295 r.add(event);
296 }
297 }
298 return r;
299 }
300
301 private void assertNotYetSetUp() {
302 assertFalse("server is not running", server.isRunning());
303 }
304
305 private void assertAlreadySetUp() {
306 assertTrue("server is running", server.isRunning());
307 }
308 }