Search in sources :

Example 6 with DomainNameMappingBuilder

use of io.netty.util.DomainNameMappingBuilder in project netty by netty.

the class SniHandlerTest method testFallbackToDefaultContext.

@ParameterizedTest(name = "{index}: sslProvider={0}")
@MethodSource("data")
public void testFallbackToDefaultContext(SslProvider provider) throws Exception {
    SslContext nettyContext = makeSslContext(provider, false);
    SslContext leanContext = makeSslContext(provider, false);
    SslContext leanContext2 = makeSslContext(provider, false);
    try {
        DomainNameMapping<SslContext> mapping = new DomainNameMappingBuilder<SslContext>(nettyContext).add("*.netty.io", nettyContext).add("*.LEANCLOUD.CN", leanContext).add("chat4.leancloud.cn", leanContext2).build();
        SniHandler handler = new SniHandler(mapping);
        EmbeddedChannel ch = new EmbeddedChannel(handler);
        // invalid
        byte[] message = { 22, 3, 1, 0, 0 };
        try {
            // Push the handshake message.
            ch.writeInbound(Unpooled.wrappedBuffer(message));
        // TODO(scott): This should fail because the engine should reject zero length records during handshake.
        // See https://github.com/netty/netty/issues/6348.
        // fail();
        } catch (Exception e) {
        // expected
        }
        ch.close();
        // When the channel is closed the SslHandler will write an empty buffer to the channel.
        ByteBuf buf = ch.readOutbound();
        // See https://github.com/netty/netty/issues/6348.
        if (buf != null) {
            assertFalse(buf.isReadable());
            buf.release();
        }
        assertThat(ch.finish(), is(false));
        assertThat(handler.hostname(), nullValue());
        assertThat(handler.sslContext(), is(nettyContext));
    } finally {
        releaseAll(leanContext, leanContext2, nettyContext);
    }
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) DomainNameMappingBuilder(io.netty.util.DomainNameMappingBuilder) DecoderException(io.netty.handler.codec.DecoderException) SSLException(javax.net.ssl.SSLException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)6 DomainNameMappingBuilder (io.netty.util.DomainNameMappingBuilder)6 DecoderException (io.netty.handler.codec.DecoderException)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 SSLException (javax.net.ssl.SSLException)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Test (org.junit.Test)2 Executable (org.junit.jupiter.api.function.Executable)2 ByteBuf (io.netty.buffer.ByteBuf)1