|
Expand Up
|
@@ -16,27 +16,40 @@ |
|
|
package io.netty.handler.ssl; |
|
|
|
|
|
import io.netty.buffer.ByteBuf; |
|
|
import io.netty.buffer.Unpooled; |
|
|
import io.netty.channel.ChannelHandler; |
|
|
import io.netty.channel.ChannelHandlerContext; |
|
|
import io.netty.channel.ChannelInboundHandlerAdapter; |
|
|
import io.netty.channel.embedded.EmbeddedChannel; |
|
|
import io.netty.channel.socket.ChannelInputShutdownEvent; |
|
|
import io.netty.handler.codec.DecoderException; |
|
|
import io.netty.util.AsyncMapping; |
|
|
import io.netty.util.concurrent.Future; |
|
|
import io.netty.util.concurrent.Promise; |
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.junit.jupiter.api.function.Executable; |
|
|
import org.junit.jupiter.params.ParameterizedTest; |
|
|
import org.junit.jupiter.params.provider.MethodSource; |
|
|
import org.junit.jupiter.params.provider.ValueSource; |
|
|
|
|
|
import javax.net.ssl.SSLContext; |
|
|
import javax.net.ssl.SSLEngine; |
|
|
import javax.net.ssl.SSLHandshakeException; |
|
|
import java.nio.channels.ClosedChannelException; |
|
|
import java.security.NoSuchAlgorithmException; |
|
|
import java.util.Arrays; |
|
|
import java.util.List; |
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
import java.util.concurrent.atomic.AtomicReference; |
|
|
|
|
|
import static io.netty.handler.ssl.CloseNotifyTest.assertCloseNotify; |
|
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
|
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
|
import static org.junit.jupiter.api.Assertions.assertFalse; |
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
|
import static org.junit.jupiter.api.Assertions.assertNull; |
|
|
import static org.junit.jupiter.api.Assertions.assertSame; |
|
|
import static org.junit.jupiter.api.Assertions.assertThrows; |
|
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
import static org.junit.jupiter.api.Assertions.fail; |
|
Expand Down
Expand Up
|
@@ -70,24 +83,193 @@ protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
|
|
assertFalse(channel.finishAndReleaseAll()); |
|
|
} |
|
|
|
|
|
@Test |
|
|
public void testHandshakeFailure() { |
|
|
@ParameterizedTest |
|
|
@ValueSource(booleans = {false, true}) |
|
|
public void testHandshakeFailure(final boolean exceptionDuringEvent) { |
|
|
final AtomicInteger failureCount = new AtomicInteger(); |
|
|
final AtomicInteger failureCountOnExceptionReturn = new AtomicInteger(); |
|
|
final AtomicInteger downstreamExceptionCount = new AtomicInteger(); |
|
|
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>(); |
|
|
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
|
|
@Override |
|
|
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
|
|
fail(); |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) { |
|
|
failureCount.incrementAndGet(); |
|
|
failureCause.set(cause); |
|
|
} |
|
|
|
|
|
@Override |
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
|
|
super.exceptionCaught(ctx, cause); |
|
|
failureCountOnExceptionReturn.set(failureCount.get()); |
|
|
} |
|
|
}; |
|
|
|
|
|
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler); |
|
|
SSLHandshakeException exception = new SSLHandshakeException("error"); |
|
|
SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception); |
|
|
final SSLHandshakeException exception = new SSLHandshakeException("error"); |
|
|
final SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception); |
|
|
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler, new ChannelInboundHandlerAdapter() { |
|
|
@Override |
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { |
|
|
if (exceptionDuringEvent && evt instanceof SslHandshakeCompletionEvent) { |
|
|
ctx.pipeline().fireExceptionCaught(new DecoderException(exception)); |
|
|
} |
|
|
|
|
|
ctx.fireUserEventTriggered(evt); |
|
|
} |
|
|
}, new ChannelInboundHandlerAdapter() { |
|
|
@Override |
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
|
|
downstreamExceptionCount.incrementAndGet(); |
|
|
} |
|
|
}); |
|
|
|
|
|
channel.pipeline().fireUserEventTriggered(completionEvent); |
|
|
|
|
|
if (!exceptionDuringEvent) { |
|
|
channel.pipeline().fireExceptionCaught(new DecoderException(exception)); |
|
|
} |
|
|
|
|
|
assertEquals(1, failureCount.get()); |
|
|
assertEquals(1, failureCountOnExceptionReturn.get()); |
|
|
assertEquals(0, downstreamExceptionCount.get()); |
|
|
assertSame(exception, failureCause.get()); |
|
|
assertNull(channel.pipeline().context(alpnHandler)); |
|
|
assertFalse(channel.finishAndReleaseAll()); |
|
|
} |
|
|
|
|
|
@ParameterizedTest |
|
|
@MethodSource("handshakeFailureCauses") |
|
|
public void testHandshakeFailureEvent(final Throwable cause) { |
|
|
final AtomicReference<SslHandshakeCompletionEvent> eventRef = |
|
|
new AtomicReference<SslHandshakeCompletionEvent>(); |
|
|
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>(); |
|
|
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
|
|
@Override |
|
|
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
|
|
fail(); |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
|
|
failureCause.set(cause); |
|
|
super.handshakeFailure(ctx, cause); |
|
|
} |
|
|
}; |
|
|
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler, new ChannelInboundHandlerAdapter() { |
|
|
@Override |
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { |
|
|
if (evt instanceof SslHandshakeCompletionEvent) { |
|
|
eventRef.set((SslHandshakeCompletionEvent) evt); |
|
|
} |
|
|
} |
|
|
}); |
|
|
final SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(cause); |
|
|
|
|
|
channel.pipeline().fireUserEventTriggered(completionEvent); |
|
|
|
|
|
assertSame(cause, failureCause.get()); |
|
|
assertSame(completionEvent, eventRef.get()); |
|
|
assertFalse(channel.isOpen()); |
|
|
assertFalse(channel.finishAndReleaseAll()); |
|
|
} |
|
|
|
|
|
private static List<Throwable> handshakeFailureCauses() { |
|
|
return Arrays.<Throwable>asList( |
|
|
new SslHandshakeTimeoutException("timed out"), |
|
|
new ClosedChannelException()); |
|
|
} |
|
|
|
|
|
@Test |
|
|
public void testHandshakeFailureExceptionWithoutEvent() { |
|
|
final AtomicInteger failureCount = new AtomicInteger(); |
|
|
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>(); |
|
|
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
|
|
@Override |
|
|
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
|
|
fail(); |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) { |
|
|
failureCount.incrementAndGet(); |
|
|
failureCause.set(cause); |
|
|
} |
|
|
}; |
|
|
EmbeddedChannel channel = new EmbeddedChannel(alpnHandler); |
|
|
final SSLHandshakeException exception = new SSLHandshakeException("error"); |
|
|
|
|
|
channel.pipeline().fireExceptionCaught(new DecoderException(exception)); |
|
|
|
|
|
assertEquals(1, failureCount.get()); |
|
|
assertSame(exception, failureCause.get()); |
|
|
assertNull(channel.pipeline().context(alpnHandler)); |
|
|
assertFalse(channel.finishAndReleaseAll()); |
|
|
} |
|
|
|
|
|
@Test |
|
|
public void testSniLookupFailure() { |
|
|
final SSLHandshakeException exception = new SSLHandshakeException("SNI lookup failed"); |
|
|
final AtomicInteger failureCount = new AtomicInteger(); |
|
|
final AtomicInteger handshakeEventCount = new AtomicInteger(); |
|
|
final AtomicInteger downstreamExceptionCount = new AtomicInteger(); |
|
|
final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>(); |
|
|
final AtomicReference<SniCompletionEvent> sniEvent = new AtomicReference<SniCompletionEvent>(); |
|
|
ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
|
|
@Override |
|
|
protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
|
|
fail(); |
|
|
} |
|
|
|
|
|
@Override |
|
|
protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) { |
|
|
failureCount.incrementAndGet(); |
|
|
failureCause.set(cause); |
|
|
} |
|
|
}; |
|
|
final SniHandler sniHandler = new SniHandler(new AsyncMapping<String, SslContext>() { |
|
|
@Override |
|
|
public Future<SslContext> map(String input, Promise<SslContext> promise) { |
|
|
return promise.setFailure(exception); |
|
|
} |
|
|
}); |
|
|
EmbeddedChannel channel = new EmbeddedChannel(sniHandler, alpnHandler, new ChannelInboundHandlerAdapter() { |
|
|
@Override |
|
|
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { |
|
|
if (evt instanceof SniCompletionEvent) { |
|
|
sniEvent.set((SniCompletionEvent) evt); |
|
|
} else if (evt instanceof SslHandshakeCompletionEvent) { |
|
|
handshakeEventCount.incrementAndGet(); |
|
|
} |
|
|
|
|
|
ctx.fireUserEventTriggered(evt); |
|
|
} |
|
|
|
|
|
@Override |
|
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
|
|
downstreamExceptionCount.incrementAndGet(); |
|
|
} |
|
|
}); |
|
|
|
|
|
try { |
|
|
channel.writeInbound(Unpooled.buffer(5).writeByte(SslUtils.SSL_CONTENT_TYPE_APPLICATION_DATA) |
|
|
.writeShort(0x0303).writeShort(0)); |
|
|
|
|
|
assertNotNull(sniEvent.get()); |
|
|
assertSame(exception, sniEvent.get().cause()); |
|
|
assertSame(exception, failureCause.get()); |
|
|
assertEquals(1, failureCount.get()); |
|
|
assertEquals(0, handshakeEventCount.get()); |
|
|
assertEquals(0, downstreamExceptionCount.get()); |
|
|
} finally { |
|
|
channel.pipeline().remove(sniHandler); |
|
|
channel.finishAndReleaseAll(); |
|
|
} |
|
|
} |
|
|
|
|
|
@Test |
|
|
public void testHandshakeSuccess() throws NoSuchAlgorithmException { |
|
|
testHandshakeSuccess0(false); |
|
Expand Down
|
|