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