|
16 | 16 | package io.netty.handler.ssl; |
17 | 17 |
|
18 | 18 | import io.netty.buffer.ByteBuf; |
| 19 | +import io.netty.buffer.Unpooled; |
19 | 20 | import io.netty.channel.ChannelHandler; |
20 | 21 | import io.netty.channel.ChannelHandlerContext; |
21 | 22 | import io.netty.channel.ChannelInboundHandlerAdapter; |
22 | 23 | import io.netty.channel.embedded.EmbeddedChannel; |
23 | 24 | import io.netty.channel.socket.ChannelInputShutdownEvent; |
24 | 25 | import io.netty.handler.codec.DecoderException; |
| 26 | +import io.netty.util.AsyncMapping; |
| 27 | +import io.netty.util.concurrent.Future; |
| 28 | +import io.netty.util.concurrent.Promise; |
25 | 29 | import org.junit.jupiter.api.Test; |
26 | 30 | import org.junit.jupiter.api.function.Executable; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
| 33 | +import org.junit.jupiter.params.provider.ValueSource; |
27 | 34 |
|
28 | 35 | import javax.net.ssl.SSLContext; |
29 | 36 | import javax.net.ssl.SSLEngine; |
30 | 37 | import javax.net.ssl.SSLHandshakeException; |
| 38 | +import java.nio.channels.ClosedChannelException; |
31 | 39 | import java.security.NoSuchAlgorithmException; |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.List; |
32 | 42 | import java.util.concurrent.atomic.AtomicBoolean; |
| 43 | +import java.util.concurrent.atomic.AtomicInteger; |
33 | 44 | import java.util.concurrent.atomic.AtomicReference; |
34 | 45 |
|
35 | 46 | import static io.netty.handler.ssl.CloseNotifyTest.assertCloseNotify; |
36 | 47 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
37 | 48 | import static org.junit.jupiter.api.Assertions.assertEquals; |
38 | 49 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 50 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
39 | 51 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertSame; |
40 | 53 | import static org.junit.jupiter.api.Assertions.assertThrows; |
41 | 54 | import static org.junit.jupiter.api.Assertions.assertTrue; |
42 | 55 | import static org.junit.jupiter.api.Assertions.fail; |
@@ -70,24 +83,193 @@ protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
70 | 83 | assertFalse(channel.finishAndReleaseAll()); |
71 | 84 | } |
72 | 85 |
|
73 | | - @Test |
74 | | - public void testHandshakeFailure() { |
| 86 | + @ParameterizedTest |
| 87 | + @ValueSource(booleans = {false, true}) |
| 88 | + public void testHandshakeFailure(final boolean exceptionDuringEvent) { |
| 89 | + final AtomicInteger failureCount = new AtomicInteger(); |
| 90 | + final AtomicInteger failureCountOnExceptionReturn = new AtomicInteger(); |
| 91 | + final AtomicInteger downstreamExceptionCount = new AtomicInteger(); |
| 92 | + final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>(); |
75 | 93 | ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
76 | 94 | @Override |
77 | 95 | protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
78 | 96 | fail(); |
79 | 97 | } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) { |
| 101 | + failureCount.incrementAndGet(); |
| 102 | + failureCause.set(cause); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| 107 | + super.exceptionCaught(ctx, cause); |
| 108 | + failureCountOnExceptionReturn.set(failureCount.get()); |
| 109 | + } |
80 | 110 | }; |
81 | 111 |
|
82 | | - EmbeddedChannel channel = new EmbeddedChannel(alpnHandler); |
83 | | - SSLHandshakeException exception = new SSLHandshakeException("error"); |
84 | | - SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception); |
| 112 | + final SSLHandshakeException exception = new SSLHandshakeException("error"); |
| 113 | + final SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception); |
| 114 | + EmbeddedChannel channel = new EmbeddedChannel(alpnHandler, new ChannelInboundHandlerAdapter() { |
| 115 | + @Override |
| 116 | + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { |
| 117 | + if (exceptionDuringEvent && evt instanceof SslHandshakeCompletionEvent) { |
| 118 | + ctx.pipeline().fireExceptionCaught(new DecoderException(exception)); |
| 119 | + } |
| 120 | + |
| 121 | + ctx.fireUserEventTriggered(evt); |
| 122 | + } |
| 123 | + }, new ChannelInboundHandlerAdapter() { |
| 124 | + @Override |
| 125 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 126 | + downstreamExceptionCount.incrementAndGet(); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + channel.pipeline().fireUserEventTriggered(completionEvent); |
| 131 | + |
| 132 | + if (!exceptionDuringEvent) { |
| 133 | + channel.pipeline().fireExceptionCaught(new DecoderException(exception)); |
| 134 | + } |
| 135 | + |
| 136 | + assertEquals(1, failureCount.get()); |
| 137 | + assertEquals(1, failureCountOnExceptionReturn.get()); |
| 138 | + assertEquals(0, downstreamExceptionCount.get()); |
| 139 | + assertSame(exception, failureCause.get()); |
| 140 | + assertNull(channel.pipeline().context(alpnHandler)); |
| 141 | + assertFalse(channel.finishAndReleaseAll()); |
| 142 | + } |
| 143 | + |
| 144 | + @ParameterizedTest |
| 145 | + @MethodSource("handshakeFailureCauses") |
| 146 | + public void testHandshakeFailureEvent(final Throwable cause) { |
| 147 | + final AtomicReference<SslHandshakeCompletionEvent> eventRef = |
| 148 | + new AtomicReference<SslHandshakeCompletionEvent>(); |
| 149 | + final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>(); |
| 150 | + ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
| 151 | + @Override |
| 152 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
| 153 | + fail(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception { |
| 158 | + failureCause.set(cause); |
| 159 | + super.handshakeFailure(ctx, cause); |
| 160 | + } |
| 161 | + }; |
| 162 | + EmbeddedChannel channel = new EmbeddedChannel(alpnHandler, new ChannelInboundHandlerAdapter() { |
| 163 | + @Override |
| 164 | + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { |
| 165 | + if (evt instanceof SslHandshakeCompletionEvent) { |
| 166 | + eventRef.set((SslHandshakeCompletionEvent) evt); |
| 167 | + } |
| 168 | + } |
| 169 | + }); |
| 170 | + final SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(cause); |
| 171 | + |
85 | 172 | channel.pipeline().fireUserEventTriggered(completionEvent); |
| 173 | + |
| 174 | + assertSame(cause, failureCause.get()); |
| 175 | + assertSame(completionEvent, eventRef.get()); |
| 176 | + assertFalse(channel.isOpen()); |
| 177 | + assertFalse(channel.finishAndReleaseAll()); |
| 178 | + } |
| 179 | + |
| 180 | + private static List<Throwable> handshakeFailureCauses() { |
| 181 | + return Arrays.<Throwable>asList( |
| 182 | + new SslHandshakeTimeoutException("timed out"), |
| 183 | + new ClosedChannelException()); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void testHandshakeFailureExceptionWithoutEvent() { |
| 188 | + final AtomicInteger failureCount = new AtomicInteger(); |
| 189 | + final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>(); |
| 190 | + ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
| 191 | + @Override |
| 192 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
| 193 | + fail(); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) { |
| 198 | + failureCount.incrementAndGet(); |
| 199 | + failureCause.set(cause); |
| 200 | + } |
| 201 | + }; |
| 202 | + EmbeddedChannel channel = new EmbeddedChannel(alpnHandler); |
| 203 | + final SSLHandshakeException exception = new SSLHandshakeException("error"); |
| 204 | + |
86 | 205 | channel.pipeline().fireExceptionCaught(new DecoderException(exception)); |
| 206 | + |
| 207 | + assertEquals(1, failureCount.get()); |
| 208 | + assertSame(exception, failureCause.get()); |
87 | 209 | assertNull(channel.pipeline().context(alpnHandler)); |
88 | 210 | assertFalse(channel.finishAndReleaseAll()); |
89 | 211 | } |
90 | 212 |
|
| 213 | + @Test |
| 214 | + public void testSniLookupFailure() { |
| 215 | + final SSLHandshakeException exception = new SSLHandshakeException("SNI lookup failed"); |
| 216 | + final AtomicInteger failureCount = new AtomicInteger(); |
| 217 | + final AtomicInteger handshakeEventCount = new AtomicInteger(); |
| 218 | + final AtomicInteger downstreamExceptionCount = new AtomicInteger(); |
| 219 | + final AtomicReference<Throwable> failureCause = new AtomicReference<Throwable>(); |
| 220 | + final AtomicReference<SniCompletionEvent> sniEvent = new AtomicReference<SniCompletionEvent>(); |
| 221 | + ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
| 222 | + @Override |
| 223 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
| 224 | + fail(); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) { |
| 229 | + failureCount.incrementAndGet(); |
| 230 | + failureCause.set(cause); |
| 231 | + } |
| 232 | + }; |
| 233 | + final SniHandler sniHandler = new SniHandler(new AsyncMapping<String, SslContext>() { |
| 234 | + @Override |
| 235 | + public Future<SslContext> map(String input, Promise<SslContext> promise) { |
| 236 | + return promise.setFailure(exception); |
| 237 | + } |
| 238 | + }); |
| 239 | + EmbeddedChannel channel = new EmbeddedChannel(sniHandler, alpnHandler, new ChannelInboundHandlerAdapter() { |
| 240 | + @Override |
| 241 | + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { |
| 242 | + if (evt instanceof SniCompletionEvent) { |
| 243 | + sniEvent.set((SniCompletionEvent) evt); |
| 244 | + } else if (evt instanceof SslHandshakeCompletionEvent) { |
| 245 | + handshakeEventCount.incrementAndGet(); |
| 246 | + } |
| 247 | + |
| 248 | + ctx.fireUserEventTriggered(evt); |
| 249 | + } |
| 250 | + |
| 251 | + @Override |
| 252 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 253 | + downstreamExceptionCount.incrementAndGet(); |
| 254 | + } |
| 255 | + }); |
| 256 | + |
| 257 | + try { |
| 258 | + channel.writeInbound(Unpooled.buffer(5).writeByte(SslUtils.SSL_CONTENT_TYPE_APPLICATION_DATA) |
| 259 | + .writeShort(0x0303).writeShort(0)); |
| 260 | + |
| 261 | + assertNotNull(sniEvent.get()); |
| 262 | + assertSame(exception, sniEvent.get().cause()); |
| 263 | + assertSame(exception, failureCause.get()); |
| 264 | + assertEquals(1, failureCount.get()); |
| 265 | + assertEquals(0, handshakeEventCount.get()); |
| 266 | + assertEquals(0, downstreamExceptionCount.get()); |
| 267 | + } finally { |
| 268 | + channel.pipeline().remove(sniHandler); |
| 269 | + channel.finishAndReleaseAll(); |
| 270 | + } |
| 271 | + } |
| 272 | + |
91 | 273 | @Test |
92 | 274 | public void testHandshakeSuccess() throws NoSuchAlgorithmException { |
93 | 275 | testHandshakeSuccess0(false); |
|