From f841056752414939969bbe3780aab899578d82cb Mon Sep 17 00:00:00 2001 From: Luke Wood Date: Thu, 16 May 2013 10:48:19 +0100 Subject: [PATCH] Fix SnappyFramedDecoder checksum validation and add a pair of suitable unit tests --- .../compression/SnappyFramedDecoder.java | 2 +- .../compression/SnappyFramedDecoderTest.java | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/codec/src/main/java/io/netty/handler/codec/compression/SnappyFramedDecoder.java b/codec/src/main/java/io/netty/handler/codec/compression/SnappyFramedDecoder.java index 3ba06a578f..6c26d67292 100644 --- a/codec/src/main/java/io/netty/handler/codec/compression/SnappyFramedDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/compression/SnappyFramedDecoder.java @@ -166,7 +166,7 @@ public class SnappyFramedDecoder extends ByteToByteDecoder { } finally { in.writerIndex(oldWriterIndex); } - int uncompressedLength = in.writerIndex() - uncompressedStart; + int uncompressedLength = out.writerIndex() - uncompressedStart; validateChecksum(checksum, out, uncompressedStart, uncompressedLength); } else { snappy.decode(in.readSlice(chunkLength - 4), out); diff --git a/codec/src/test/java/io/netty/handler/codec/compression/SnappyFramedDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/SnappyFramedDecoderTest.java index 5b9021bd8c..37d068c34a 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/SnappyFramedDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/SnappyFramedDecoderTest.java @@ -126,4 +126,33 @@ public class SnappyFramedDecoderTest { ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' }); assertEquals(expected, channel.readInbound()); } + + // The following two tests differ in only the checksum provided for the literal + // uncompressed string "netty" + + @Test(expected = CompressionException.class) + public void testInvalidChecksumThrowsException() throws Exception { + EmbeddedByteChannel channel = new EmbeddedByteChannel(new SnappyFramedDecoder(true)); + + // checksum here is presented as 0 + ByteBuf in = Unpooled.wrappedBuffer(new byte[] { + -0x80, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, + 0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y' + }); + + channel.writeInbound(in); + } + + @Test + public void testInvalidChecksumDoesNotThrowException() throws Exception { + EmbeddedByteChannel channel = new EmbeddedByteChannel(new SnappyFramedDecoder(true)); + + // checksum here is presented as -1568496083 (little endian) + ByteBuf in = Unpooled.wrappedBuffer(new byte[] { + -0x80, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59, + 0x01, 0x09, 0x00, 0x00, 0x2d, -0x5a, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y' + }); + + channel.writeInbound(in); + } }