|
| 1 | +/* |
| 2 | + * Copyright 2020 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.handler.ssl; |
| 17 | + |
| 18 | +import io.netty.channel.ChannelHandler; |
| 19 | +import io.netty.channel.ChannelHandlerContext; |
| 20 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 21 | +import io.netty.handler.codec.DecoderException; |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import javax.net.ssl.SSLContext; |
| 25 | +import javax.net.ssl.SSLEngine; |
| 26 | +import javax.net.ssl.SSLHandshakeException; |
| 27 | +import java.security.NoSuchAlgorithmException; |
| 28 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertEquals; |
| 31 | +import static org.junit.Assert.assertFalse; |
| 32 | +import static org.junit.Assert.assertNull; |
| 33 | +import static org.junit.Assert.assertTrue; |
| 34 | +import static org.junit.Assert.fail; |
| 35 | + |
| 36 | + |
| 37 | +public class ApplicationProtocolNegotiationHandlerTest { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testHandshakeFailure() { |
| 41 | + ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
| 42 | + @Override |
| 43 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
| 44 | + fail(); |
| 45 | + } |
| 46 | + }; |
| 47 | + |
| 48 | + EmbeddedChannel channel = new EmbeddedChannel(alpnHandler); |
| 49 | + SSLHandshakeException exception = new SSLHandshakeException("error"); |
| 50 | + SslHandshakeCompletionEvent completionEvent = new SslHandshakeCompletionEvent(exception); |
| 51 | + channel.pipeline().fireUserEventTriggered(completionEvent); |
| 52 | + channel.pipeline().fireExceptionCaught(new DecoderException(exception)); |
| 53 | + assertNull(channel.pipeline().context(alpnHandler)); |
| 54 | + assertFalse(channel.finishAndReleaseAll()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testHandshakeSuccess() throws NoSuchAlgorithmException { |
| 59 | + final AtomicBoolean configureCalled = new AtomicBoolean(false); |
| 60 | + ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
| 61 | + @Override |
| 62 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
| 63 | + configureCalled.set(true); |
| 64 | + assertEquals(ApplicationProtocolNames.HTTP_1_1, protocol); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + SSLEngine engine = SSLContext.getDefault().createSSLEngine(); |
| 69 | + engine.setUseClientMode(false); |
| 70 | + |
| 71 | + EmbeddedChannel channel = new EmbeddedChannel(new SslHandler(engine), alpnHandler); |
| 72 | + channel.pipeline().fireUserEventTriggered(SslHandshakeCompletionEvent.SUCCESS); |
| 73 | + assertNull(channel.pipeline().context(alpnHandler)); |
| 74 | + // Should produce the close_notify messages |
| 75 | + assertTrue(channel.finishAndReleaseAll()); |
| 76 | + assertTrue(configureCalled.get()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test(expected = IllegalStateException.class) |
| 80 | + public void testHandshakeSuccessButNoSslHandler() { |
| 81 | + ChannelHandler alpnHandler = new ApplicationProtocolNegotiationHandler(ApplicationProtocolNames.HTTP_1_1) { |
| 82 | + @Override |
| 83 | + protected void configurePipeline(ChannelHandlerContext ctx, String protocol) { |
| 84 | + fail(); |
| 85 | + } |
| 86 | + }; |
| 87 | + EmbeddedChannel channel = new EmbeddedChannel(alpnHandler); |
| 88 | + try { |
| 89 | + channel.pipeline().fireUserEventTriggered(SslHandshakeCompletionEvent.SUCCESS); |
| 90 | + } finally { |
| 91 | + assertNull(channel.pipeline().context(alpnHandler)); |
| 92 | + assertFalse(channel.finishAndReleaseAll()); |
| 93 | + } |
| 94 | + } |
| 95 | +} |