-
Notifications
You must be signed in to change notification settings - Fork 974
/
DefaultServer.java
819 lines (725 loc) · 37.1 KB
/
DefaultServer.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.undertow.testutils;
import static io.undertow.server.handlers.ResponseCodeHandler.HANDLE_404;
import static org.xnio.Options.SSL_CLIENT_AUTH_MODE;
import static org.xnio.SslClientAuthMode.REQUESTED;
import java.io.IOException;
import java.io.InputStream;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.URI;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import org.junit.Assume;
import org.junit.Ignore;
import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
import org.wildfly.openssl.OpenSSLProvider;
import org.xnio.ChannelListener;
import org.xnio.ChannelListeners;
import org.xnio.IoUtils;
import org.xnio.OptionMap;
import org.xnio.Options;
import org.xnio.Sequence;
import org.xnio.StreamConnection;
import org.xnio.Xnio;
import org.xnio.XnioWorker;
import org.xnio.channels.AcceptingChannel;
import org.xnio.ssl.XnioSsl;
import io.undertow.UndertowLogger;
import io.undertow.UndertowOptions;
import io.undertow.connector.ByteBufferPool;
import io.undertow.protocols.alpn.ALPNManager;
import io.undertow.protocols.alpn.ALPNProvider;
import io.undertow.protocols.alpn.JettyAlpnProvider;
import io.undertow.protocols.ssl.SNIContextMatcher;
import io.undertow.protocols.ssl.SNISSLContext;
import io.undertow.protocols.ssl.UndertowXnioSsl;
import io.undertow.security.impl.GSSAPIAuthenticationMechanism;
import io.undertow.server.DefaultByteBufferPool;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.OpenListener;
import io.undertow.server.handlers.ProxyPeerAddressHandler;
import io.undertow.server.handlers.RequestDumpingHandler;
import io.undertow.server.handlers.SSLHeaderHandler;
import io.undertow.server.handlers.proxy.LoadBalancingProxyClient;
import io.undertow.server.handlers.proxy.ProxyHandler;
import io.undertow.server.protocol.ajp.AjpOpenListener;
import io.undertow.server.protocol.http.AlpnOpenListener;
import io.undertow.server.protocol.http.HttpOpenListener;
import io.undertow.server.protocol.http2.Http2OpenListener;
import io.undertow.server.protocol.http2.Http2UpgradeHandler;
import io.undertow.util.Headers;
import io.undertow.util.NetworkUtils;
import io.undertow.util.SingleByteStreamSinkConduit;
import io.undertow.util.SingleByteStreamSourceConduit;
/**
* A class that starts a server before the test suite. By swapping out the root handler
* tests can test various server functionality without continually starting and stopping the server.
*
* @author Stuart Douglas
*/
public class DefaultServer extends BlockJUnit4ClassRunner {
private static final Throwable OPENSSL_FAILURE;
static {
Throwable failure = null;
try {
OpenSSLProvider.register();
} catch (Throwable t) {
failure = t;
}
OPENSSL_FAILURE = failure;
}
static final String DEFAULT = "default";
private static final int PROXY_OFFSET = 1111;
public static final int APACHE_PORT = 9080;
public static final int APACHE_SSL_PORT = 9443;
public static final int BUFFER_SIZE = Integer.getInteger("test.bufferSize", 1024 * 16 - 20);
public static final DebuggingSlicePool SSL_BUFFER_POOL = new DebuggingSlicePool(new DefaultByteBufferPool(true, 17 * 1024));
private static boolean first = true;
private static OptionMap serverOptions;
private static OpenListener openListener;
private static ChannelListener acceptListener;
private static OpenListener proxyOpenListener;
private static ChannelListener proxyAcceptListener;
private static XnioWorker worker;
private static AcceptingChannel<? extends StreamConnection> server;
private static AcceptingChannel<? extends StreamConnection> proxyServer;
private static AcceptingChannel<? extends StreamConnection> sslServer;
private static SSLContext clientSslContext;
private static Xnio xnio;
private static final String SERVER_KEY_STORE = "server.keystore";
private static final String SERVER_TRUST_STORE = "server.truststore";
private static final String CLIENT_KEY_STORE = "client.keystore";
private static final String CLIENT_TRUST_STORE = "client.truststore";
private static final char[] STORE_PASSWORD = "password".toCharArray();
private static final boolean ajp = Boolean.getBoolean("test.ajp");
private static final boolean h2 = Boolean.getBoolean("test.h2");
private static final boolean h2c = Boolean.getBoolean("test.h2c");
private static final boolean h2cUpgrade = Boolean.getBoolean("test.h2c-upgrade");
private static final boolean https = Boolean.getBoolean("test.https");
private static final boolean proxy = Boolean.getBoolean("test.proxy");
private static final boolean apache = Boolean.getBoolean("test.apache");
private static final boolean dump = Boolean.getBoolean("test.dump");
private static final boolean single = Boolean.getBoolean("test.single");
private static final boolean openssl = Boolean.getBoolean("test.openssl");
private static final int runs = Integer.getInteger("test.runs", 1);
private static final DelegatingHandler rootHandler = new DelegatingHandler();
private static final DebuggingSlicePool pool = new DebuggingSlicePool(new DefaultByteBufferPool(true, BUFFER_SIZE, 1000, 10, 100));
private static LoadBalancingProxyClient loadBalancingProxyClient;
private static KeyStore loadKeyStore(final String name) throws IOException {
final InputStream stream = DefaultServer.class.getClassLoader().getResourceAsStream(name);
if (stream == null) {
throw new RuntimeException("Could not load keystore");
}
try {
KeyStore loadedKeystore = KeyStore.getInstance("JKS");
loadedKeystore.load(stream, STORE_PASSWORD);
return loadedKeystore;
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException e) {
throw new IOException(String.format("Unable to load KeyStore %s", name), e);
} finally {
IoUtils.safeClose(stream);
}
}
private static SSLContext createSSLContext(final KeyStore keyStore, final KeyStore trustStore, boolean client) throws IOException {
KeyManager[] keyManagers;
try {
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, STORE_PASSWORD);
keyManagers = keyManagerFactory.getKeyManagers();
} catch (NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException e) {
throw new IOException("Unable to initialise KeyManager[]", e);
}
TrustManager[] trustManagers = null;
try {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
trustManagers = trustManagerFactory.getTrustManagers();
} catch (NoSuchAlgorithmException | KeyStoreException e) {
throw new IOException("Unable to initialise TrustManager[]", e);
}
SSLContext sslContext;
try {
if (openssl && !client) {
sslContext = SSLContext.getInstance("openssl.TLS");
} else {
sslContext = SSLContext.getInstance("TLSv1.2");
}
sslContext.init(keyManagers, trustManagers, null);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new IOException("Unable to create and initialise the SSLContext", e);
}
if (!client) {
SNIContextMatcher matcher = new SNIContextMatcher.Builder().setDefaultContext(sslContext)
.addMatch("localhost", sslContext)
.build();
return new SNISSLContext(matcher);
} else {
return sslContext;
}
}
/**
* @return The base URL that can be used to make connections to this server
*/
public static String getDefaultServerURL() {
return "http://" + NetworkUtils.formatPossibleIpv6Address(getHostAddress(DEFAULT)) + ":" + getHostPort(DEFAULT);
}
public static InetSocketAddress getDefaultServerAddress() {
return new InetSocketAddress(DefaultServer.getHostAddress("default"), DefaultServer.getHostPort("default"));
}
public static String getDefaultServerSSLAddress() {
if (sslServer == null && !isApacheTest()) {
throw new IllegalStateException("SSL Server not started.");
}
return "https://" + NetworkUtils.formatPossibleIpv6Address(getHostAddress(DEFAULT)) + ":" + getHostSSLPort(DEFAULT);
}
public DefaultServer(Class<?> klass) throws InitializationError {
super(klass);
}
public static void setupProxyHandlerForSSL(ProxyHandler proxyHandler) {
proxyHandler.addRequestHeader(Headers.SSL_CLIENT_CERT, "%{SSL_CLIENT_CERT}", DefaultServer.class.getClassLoader());
proxyHandler.addRequestHeader(Headers.SSL_CIPHER, "%{SSL_CIPHER}", DefaultServer.class.getClassLoader());
proxyHandler.addRequestHeader(Headers.SSL_SESSION_ID, "%{SSL_SESSION_ID}", DefaultServer.class.getClassLoader());
}
public static ByteBufferPool getBufferPool() {
return pool;
}
@Override
public Description getDescription() {
return super.getDescription();
}
@Override
public void run(final RunNotifier notifier) {
notifier.addListener(new RunListener() {
@Override
public void testStarted(Description description) throws Exception {
DebuggingSlicePool.currentLabel = description.getClassName() + "." + description.getMethodName();
super.testStarted(description);
}
@Override
public void testFinished(Description description) throws Exception {
if (!DebuggingSlicePool.BUFFERS.isEmpty()) {
try {
Thread.sleep(200);
long end = System.currentTimeMillis() + 20000;
while (!DebuggingSlicePool.BUFFERS.isEmpty() && System.currentTimeMillis() < end) {
Thread.sleep(200);
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
for (DebuggingSlicePool.DebuggingBuffer b : DebuggingSlicePool.BUFFERS) {
b.getAllocationPoint().printStackTrace();
notifier.fireTestFailure(new Failure(description, new RuntimeException("Buffer Leak " + b.getLabel(), b.getAllocationPoint())));
}
DebuggingSlicePool.BUFFERS.clear();
}
super.testFinished(description);
}
});
runInternal(notifier);
super.run(notifier);
}
private static void runInternal(final RunNotifier notifier) {
if (openssl && OPENSSL_FAILURE != null) {
throw new RuntimeException(OPENSSL_FAILURE);
}
if (first) {
first = false;
xnio = Xnio.getInstance("nio", DefaultServer.class.getClassLoader());
try {
worker = xnio.createWorker(OptionMap.builder()
.set(Options.WORKER_IO_THREADS, 8)
.set(Options.CONNECTION_HIGH_WATER, 1000000)
.set(Options.CONNECTION_LOW_WATER, 1000000)
.set(Options.WORKER_TASK_CORE_THREADS, 30)
.set(Options.WORKER_TASK_MAX_THREADS, 30)
.set(Options.TCP_NODELAY, true)
.set(Options.CORK, true)
.getMap());
serverOptions = OptionMap.builder()
.set(Options.TCP_NODELAY, true)
.set(Options.BACKLOG, 1000)
.set(Options.REUSE_ADDRESSES, true)
.set(Options.BALANCING_TOKENS, 1)
.set(Options.BALANCING_CONNECTIONS, 2)
.getMap();
final SSLContext serverContext = createSSLContext(loadKeyStore(SERVER_KEY_STORE), loadKeyStore(SERVER_TRUST_STORE), false);
UndertowXnioSsl ssl = new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, SSL_BUFFER_POOL, serverContext);
if (ajp) {
openListener = new AjpOpenListener(pool);
acceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(openListener));
if (apache) {
int port = 8888;
server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), port), acceptListener, serverOptions);
} else {
server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), 7777 + PROXY_OFFSET), acceptListener, serverOptions);
proxyOpenListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true));
proxyAcceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(proxyOpenListener));
proxyServer = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), getHostPort(DEFAULT)), proxyAcceptListener, serverOptions);
proxyOpenListener.setRootHandler(new ProxyHandler(loadBalancingProxyClient = new LoadBalancingProxyClient(GSSAPIAuthenticationMechanism.EXCLUSIVITY_CHECKER).addHost(new URI("ajp", null, getHostAddress(DEFAULT), getHostPort(DEFAULT) + PROXY_OFFSET, "/", null, null)), 120000, HANDLE_404));
proxyServer.resumeAccepts();
}
} else if (h2 && isAlpnEnabled()) {
openListener = new Http2OpenListener(pool, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true, UndertowOptions.HTTP2_PADDING_SIZE, 10));
acceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(new AlpnOpenListener(pool).addProtocol(Http2OpenListener.HTTP2, (io.undertow.server.DelegateOpenListener) openListener, 10)));
SSLContext clientContext = createSSLContext(loadKeyStore(CLIENT_KEY_STORE), loadKeyStore(CLIENT_TRUST_STORE), true);
server = ssl.createSslConnectionServer(worker, new InetSocketAddress(getHostAddress("default"), 7777 + PROXY_OFFSET), acceptListener, serverOptions);
server.resumeAccepts();
proxyOpenListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true));
proxyAcceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(proxyOpenListener));
proxyServer = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), getHostPort(DEFAULT)), proxyAcceptListener, serverOptions);
ProxyHandler proxyHandler = new ProxyHandler(loadBalancingProxyClient = new LoadBalancingProxyClient(GSSAPIAuthenticationMechanism.EXCLUSIVITY_CHECKER).addHost(new URI("h2", null, getHostAddress(DEFAULT), getHostPort(DEFAULT) + PROXY_OFFSET, "/", null, null), null, new UndertowXnioSsl(xnio, OptionMap.EMPTY, SSL_BUFFER_POOL, clientContext), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)), 120000, HANDLE_404);
setupProxyHandlerForSSL(proxyHandler);
proxyOpenListener.setRootHandler(proxyHandler);
proxyServer.resumeAccepts();
} else if (h2c || h2cUpgrade) {
openListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true, UndertowOptions.HTTP2_PADDING_SIZE, 10));
acceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(openListener));
InetSocketAddress targetAddress = new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), getHostPort(DEFAULT) + PROXY_OFFSET);
server = worker.createStreamConnectionServer(targetAddress, acceptListener, serverOptions);
proxyOpenListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true));
proxyAcceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(proxyOpenListener));
proxyServer = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), getHostPort(DEFAULT)), proxyAcceptListener, serverOptions);
ProxyHandler proxyHandler = new ProxyHandler(loadBalancingProxyClient = new LoadBalancingProxyClient(GSSAPIAuthenticationMechanism.EXCLUSIVITY_CHECKER).addHost(new URI(h2cUpgrade ? "http" : "h2c-prior", null, getHostAddress(DEFAULT), getHostPort(DEFAULT) + PROXY_OFFSET, "/", null, null), null, null, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)), 30000, HANDLE_404);
setupProxyHandlerForSSL(proxyHandler);
proxyOpenListener.setRootHandler(proxyHandler);
proxyServer.resumeAccepts();
} else if (https) {
XnioSsl clientSsl = new UndertowXnioSsl(xnio, OptionMap.EMPTY, SSL_BUFFER_POOL, createClientSslContext());
openListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true));
acceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(openListener));
server = ssl.createSslConnectionServer(worker, new InetSocketAddress(getHostAddress("default"), 7777 + PROXY_OFFSET), acceptListener, serverOptions);
server.getAcceptSetter().set(acceptListener);
server.resumeAccepts();
proxyOpenListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true));
proxyAcceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(proxyOpenListener));
proxyServer = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), getHostPort(DEFAULT)), proxyAcceptListener, serverOptions);
ProxyHandler proxyHandler = new ProxyHandler(loadBalancingProxyClient = new LoadBalancingProxyClient(GSSAPIAuthenticationMechanism.EXCLUSIVITY_CHECKER).addHost(new URI("https", null, getHostAddress(DEFAULT), getHostPort(DEFAULT) + PROXY_OFFSET, "/", null, null), clientSsl), 30000, HANDLE_404);
setupProxyHandlerForSSL(proxyHandler);
proxyOpenListener.setRootHandler(proxyHandler);
proxyServer.resumeAccepts();
} else {
if (h2) {
UndertowLogger.ROOT_LOGGER.error("HTTP2 selected but Netty ALPN was not on the boot class path");
}
openListener = new HttpOpenListener(pool, OptionMap.builder().set(UndertowOptions.BUFFER_PIPELINED_DATA, true).set(UndertowOptions.ENABLE_CONNECTOR_STATISTICS, true).set(UndertowOptions.REQUIRE_HOST_HTTP11, true).getMap());
acceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(openListener));
if (!proxy) {
server = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), getHostPort(DEFAULT)), acceptListener, serverOptions);
} else {
InetSocketAddress targetAddress = new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), getHostPort(DEFAULT) + PROXY_OFFSET);
server = worker.createStreamConnectionServer(targetAddress, acceptListener, serverOptions);
proxyOpenListener = new HttpOpenListener(pool, OptionMap.create(UndertowOptions.BUFFER_PIPELINED_DATA, true));
proxyAcceptListener = ChannelListeners.openListenerAdapter(wrapOpenListener(proxyOpenListener));
proxyServer = worker.createStreamConnectionServer(new InetSocketAddress(Inet4Address.getByName(getHostAddress(DEFAULT)), getHostPort(DEFAULT)), proxyAcceptListener, serverOptions);
ProxyHandler proxyHandler = new ProxyHandler(loadBalancingProxyClient = new LoadBalancingProxyClient(GSSAPIAuthenticationMechanism.EXCLUSIVITY_CHECKER).addHost(new URI("http", null, getHostAddress(DEFAULT), getHostPort(DEFAULT) + PROXY_OFFSET, "/", null, null)), 30000, HANDLE_404);
setupProxyHandlerForSSL(proxyHandler);
proxyOpenListener.setRootHandler(proxyHandler);
proxyServer.resumeAccepts();
}
}
if (h2cUpgrade) {
openListener.setRootHandler(new Http2UpgradeHandler(rootHandler));
} else {
openListener.setRootHandler(rootHandler);
}
server.resumeAccepts();
} catch (Exception e) {
throw new RuntimeException(e);
}
notifier.addListener(new RunListener() {
@Override
public void testRunFinished(final Result result) throws Exception {
server.close();
stopSSLServer();
worker.shutdown();
}
});
}
}
@Override
protected Description describeChild(FrameworkMethod method) {
if (runs > 1 && method.getAnnotation(Ignore.class) == null) {
return describeRepeatTest(method);
}
return super.describeChild(method);
}
private Description describeRepeatTest(FrameworkMethod method) {
Description description = Description.createSuiteDescription(
testName(method) + " [" + runs + " times]",
method.getAnnotations());
for (int i = 1; i <= runs; i++) {
description.addChild(Description.createTestDescription(
getTestClass().getJavaClass(),
"[" + i + "] " + testName(method)));
}
return description;
}
private static ChannelListener<StreamConnection> wrapOpenListener(final ChannelListener<StreamConnection> listener) {
if (!single) {
return listener;
}
return new ChannelListener<StreamConnection>() {
@Override
public void handleEvent(StreamConnection channel) {
channel.getSinkChannel().setConduit(new SingleByteStreamSinkConduit(channel.getSinkChannel().getConduit(), 10000));
channel.getSourceChannel().setConduit(new SingleByteStreamSourceConduit(channel.getSourceChannel().getConduit(), 10000));
listener.handleEvent(channel);
}
};
}
@Override
protected void runChild(FrameworkMethod method, RunNotifier notifier) {
AjpIgnore ajpIgnore = method.getAnnotation(AjpIgnore.class);
if (ajpIgnore == null) {
ajpIgnore = method.getMethod().getDeclaringClass().getAnnotation(AjpIgnore.class);
}
if (ajp && ajpIgnore != null) {
if (apache || !ajpIgnore.apacheOnly()) {
notifier.fireTestIgnored(describeChild(method));
return;
}
}
if (h2 || h2c || ajp || h2cUpgrade) {
//h2c-upgrade we still allow HTTP1
HttpOneOnly httpOneOnly = method.getAnnotation(HttpOneOnly.class);
if (httpOneOnly == null) {
httpOneOnly = method.getMethod().getDeclaringClass().getAnnotation(HttpOneOnly.class);
}
if (httpOneOnly != null) {
notifier.fireTestIgnored(describeChild(method));
return;
}
if (h2) {
assumeAlpnEnabled();
}
}
if (https) {
HttpsIgnore httpsIgnore = method.getAnnotation(HttpsIgnore.class);
if (httpsIgnore == null) {
httpsIgnore = method.getMethod().getDeclaringClass().getAnnotation(HttpsIgnore.class);
}
if (httpsIgnore != null) {
notifier.fireTestIgnored(describeChild(method));
return;
}
}
if (isProxy()) {
if (method.getAnnotation(ProxyIgnore.class) != null ||
method.getMethod().getDeclaringClass().isAnnotationPresent(ProxyIgnore.class) ||
getTestClass().getJavaClass().isAnnotationPresent(ProxyIgnore.class)) {
notifier.fireTestIgnored(describeChild(method));
return;
}
}
try {
if (runs > 1) {
Statement statement = methodBlock(method);
Description description = describeChild(method);
for (Description desc : description.getChildren()) {
runLeaf(statement, desc, notifier);
}
} else {
super.runChild(method, notifier);
}
} finally {
TestHttpClient.afterTest();
}
}
@Override
protected String testName(FrameworkMethod method) {
if (!isProxy()) {
return super.testName(method);
} else {
StringBuilder sb = new StringBuilder(super.testName(method));
if (isProxy()) {
sb.append("[proxy]");
}
if (ajp) {
sb.append("[ajp]");
}
if (https) {
sb.append("[https]");
}
if (h2) {
sb.append("[http2]");
}
if (h2c) {
sb.append("[http2-clear]");
}
if (h2cUpgrade) {
sb.append("[http2-clear-upgrade]");
}
return sb.toString();
}
}
/**
* Sets the root handler for the default web server
*
* @param handler The handler to use
*/
public static void setRootHandler(HttpHandler handler) {
if ((isProxy()) && !ajp) {
//if we are testing HTTP proxy we always add the SSLHeaderHandler
//this allows the SSL information to be propagated to be backend
handler = new SSLHeaderHandler(new ProxyPeerAddressHandler(handler));
}
if (dump) {
rootHandler.next = new RequestDumpingHandler(handler);
} else {
rootHandler.next = handler;
}
}
/**
* When using the default SSL settings returns the corresponding client context.
* <p/>
* If a test case is initialising a custom server side SSLContext then the test case will be responsible for creating it's
* own client side.
*
* @return The client side SSLContext.
*/
public static SSLContext getClientSSLContext() {
if (clientSslContext == null) {
clientSslContext = createClientSslContext();
}
return clientSslContext;
}
/**
* Start the SSL server using the default settings.
* <p/>
* The default settings initialise a server with a key for 'localhost' and a trust store containing the certificate of a
* single client, the client authentication mode is set to 'REQUESTED' to optionally allow progression to CLIENT-CERT
* authentication.
*/
public static void startSSLServer() throws IOException {
SSLContext serverContext = getServerSslContext();
getClientSSLContext();
startSSLServer(serverContext, OptionMap.create(SSL_CLIENT_AUTH_MODE, REQUESTED, Options.SSL_ENABLED_PROTOCOLS, Sequence.of("TLSv1.2")));
}
public static SSLContext createClientSslContext() {
try {
return createSSLContext(loadKeyStore(CLIENT_KEY_STORE), loadKeyStore(CLIENT_TRUST_STORE), true);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SSLContext getServerSslContext() {
try {
return createSSLContext(loadKeyStore(SERVER_KEY_STORE), loadKeyStore(SERVER_TRUST_STORE), false);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Start the SSL server using the default ssl context and the provided option map
* <p/>
* The default settings initialise a server with a key for 'localhost' and a trust store containing the certificate of a
* single client. Client cert mode is not set by default
*/
public static void startSSLServer(OptionMap optionMap) throws IOException {
clientSslContext = createClientSslContext();
startSSLServer(optionMap, proxyAcceptListener != null ? proxyAcceptListener : acceptListener);
}
/**
* Start the SSL server using the default ssl context and the provided option map
* <p/>
* The default settings initialise a server with a key for 'localhost' and a trust store containing the certificate of a
* single client. Client cert mode is not set by default
*/
public static void startSSLServer(OptionMap optionMap, ChannelListener openListener) throws IOException {
SSLContext serverContext = createSSLContext(loadKeyStore(SERVER_KEY_STORE), loadKeyStore(SERVER_TRUST_STORE), false);
clientSslContext = createSSLContext(loadKeyStore(CLIENT_KEY_STORE), loadKeyStore(CLIENT_TRUST_STORE), true);
startSSLServer(serverContext, optionMap, openListener);
}
/**
* Start the SSL server using a custom SSLContext with additional options to pass to the JsseXnioSsl instance.
*
* @param context - The SSLContext to use for JsseXnioSsl initialisation.
* @param options - Additional options to be passed to the JsseXnioSsl, this will be merged with the default options where
* applicable.
*/
public static void startSSLServer(final SSLContext context, final OptionMap options) throws IOException {
startSSLServer(context, options, proxyAcceptListener != null ? proxyAcceptListener : acceptListener);
}
/**
* Start the SSL server using a custom SSLContext with additional options to pass to the JsseXnioSsl instance.
*
* @param context - The SSLContext to use for JsseXnioSsl initialisation.
* @param options - Additional options to be passed to the JsseXnioSsl, this will be merged with the default options where
* applicable.
*/
public static void startSSLServer(final SSLContext context, final OptionMap options, ChannelListener openListener) throws IOException {
startSSLServer(context, options, openListener, getHostSSLPort(DEFAULT));
}
/**
* Start the SSL server using a custom SSLContext with additional options to pass to the JsseXnioSsl instance.
*
* @param context - The SSLContext to use for JsseXnioSsl initialisation.
* @param options - Additional options to be passed to the JsseXnioSsl, this will be merged with the default options where
* applicable.
*/
public static void startSSLServer(final SSLContext context, final OptionMap options, ChannelListener openListener, int port) throws IOException {
if (isApacheTest()) {
return;
}
OptionMap combined = OptionMap.builder().addAll(serverOptions).addAll(options)
.set(Options.USE_DIRECT_BUFFERS, true)
.getMap();
UndertowXnioSsl ssl = new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, SSL_BUFFER_POOL, context);
sslServer = ssl.createSslConnectionServer(worker, new InetSocketAddress(getHostAddress("default"), port), openListener, combined);
sslServer.getAcceptSetter().set(openListener);
sslServer.resumeAccepts();
}
private static boolean isApacheTest() {
return apache;
}
/**
* Stop any previously created SSL server - as this is for test clean up calling when no SSL server is running will not
* cause an error.
*/
public static void stopSSLServer() throws IOException {
if (sslServer != null) {
sslServer.close();
sslServer = null;
}
clientSslContext = null;
if(proxyOpenListener != null) {
proxyOpenListener.closeConnections();
} else {
openListener.closeConnections();
}
}
public static String getHostAddress(String serverName) {
return System.getProperty(serverName + ".server.address", "localhost");
}
public static String getHostAddress() {
return getHostAddress(DEFAULT);
}
public static int getHostPort(String serverName) {
if (isApacheTest()) {
return APACHE_PORT;
}
return Integer.getInteger(serverName + ".server.port", 7777);
}
public static int getHostPort() {
return getHostPort(DEFAULT);
}
public static int getHostSSLPort(String serverName) {
if (isApacheTest()) {
return APACHE_SSL_PORT;
}
return Integer.getInteger(serverName + ".server.sslPort", 7778);
}
public static OptionMap getUndertowOptions() {
return openListener.getUndertowOptions();
}
public static void setUndertowOptions(final OptionMap options) {
OptionMap.Builder builder = OptionMap.builder().addAll(options);
if (h2c) {
builder.set(UndertowOptions.ENABLE_HTTP2, true);
}
openListener.setUndertowOptions(builder.getMap());
openListener.closeConnections();
if(proxyOpenListener != null) {
proxyOpenListener.closeConnections();
}
if (loadBalancingProxyClient != null) {
loadBalancingProxyClient.closeCurrentConnections();
}
}
public static XnioWorker getWorker() {
return worker;
}
public static class Parameterized extends org.junit.runners.Parameterized {
public Parameterized(Class<?> klass) throws Throwable {
super(klass);
}
@Override
public void run(final RunNotifier notifier) {
runInternal(notifier);
super.run(notifier);
}
}
public static boolean isAjp() {
return ajp;
}
public static boolean isProxy() {
return proxy || https || h2 || h2c || ajp || h2cUpgrade;
}
public static boolean isHttps() {
return https;
}
public static boolean isH2() {
return h2 || h2c || h2cUpgrade;
}
public static boolean isH2upgrade() {
return h2cUpgrade;
}
/**
* The root handler is tied to the connection, and AJP can re-use connections for different tests, so we
* use a delegating handler to chance the next handler after the root.
* <p/>
* TODO: should we re-read the root handler for every request?
*/
private static final class DelegatingHandler implements HttpHandler {
volatile HttpHandler next;
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
next.handleRequest(exchange);
}
}
private static Boolean alpnEnabled;
private static boolean isAlpnEnabled() {
if (alpnEnabled == null) {
//we use the client context, as the server one is wrapped by a SNISSLEngine
//so we can't tell that ALPN is enabled or now
SSLEngine engine = getClientSSLContext().createSSLEngine();
ALPNProvider provider = ALPNManager.INSTANCE.getProvider(engine);
if (provider instanceof JettyAlpnProvider) {
alpnEnabled = System.getProperty("alpn-boot-string") != null;
} else {
alpnEnabled = provider != null;
}
}
return alpnEnabled;
}
public static void assumeAlpnEnabled() {
Assume.assumeTrue(isAlpnEnabled());
}
}