From 02a79c51e5e3a8516ac9e715aa9a59645500dd12 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Mon, 16 Dec 2013 16:30:24 +0900 Subject: [PATCH] Fix a bug in SslHandler where a ClassCastException is raised when non-ByteBuf message is passed - Fixes #1828 --- .../java/io/netty/handler/ssl/SslHandler.java | 6 ++++++ .../io/netty/handler/ssl/SslHandlerTest.java | 20 ++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/handler/src/main/java/io/netty/handler/ssl/SslHandler.java b/handler/src/main/java/io/netty/handler/ssl/SslHandler.java index 1cbad018e6..8def5971c9 100644 --- a/handler/src/main/java/io/netty/handler/ssl/SslHandler.java +++ b/handler/src/main/java/io/netty/handler/ssl/SslHandler.java @@ -437,6 +437,12 @@ public class SslHandler extends ByteToMessageDecoder { out = ctx.alloc().buffer(maxPacketBufferSize); } + if (!(pending.msg() instanceof ByteBuf)) { + ctx.write(pending.msg(), (ChannelPromise) pending.recycleAndGet()); + pendingUnencryptedWrites.remove(); + continue; + } + ByteBuf buf = (ByteBuf) pending.msg(); SSLEngineResult result = wrap(engine, buf, out); diff --git a/handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java b/handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java index c64871e12c..dc381b3f6b 100644 --- a/handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java +++ b/handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java @@ -38,7 +38,7 @@ public class SslHandlerTest { EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(engine)); // Push the first part of a 5-byte handshake message. - ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 22, 3, 1, 0, 5 })); + ch.writeInbound(Unpooled.wrappedBuffer(new byte[]{22, 3, 1, 0, 5})); // Should decode nothing yet. assertThat(ch.readInbound(), is(nullValue())); @@ -52,4 +52,22 @@ public class SslHandlerTest { assertThat(e.getCause(), is(instanceOf(SSLProtocolException.class))); } } + + @Test + public void testNonByteBufPassthrough() throws Exception { + SSLEngine engine = SSLContext.getDefault().createSSLEngine(); + engine.setUseClientMode(false); + + EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(engine)); + + Object msg1 = new Object(); + ch.writeOutbound(msg1); + assertThat(ch.readOutbound(), is(sameInstance(msg1))); + + Object msg2 = new Object(); + ch.writeInbound(msg2); + assertThat(ch.readInbound(), is(sameInstance(msg2))); + + ch.finish(); + } }