Fix SnappyFramedDecoder checksum validation and add a pair of suitable unit tests

This commit is contained in:
Luke Wood 2013-05-16 10:48:19 +01:00
parent 2040b07849
commit f841056752
2 changed files with 30 additions and 1 deletions

View File

@ -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);

View File

@ -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);
}
}