Fix SnappyFramedDecoderTest

This commit is contained in:
Trustin Lee 2013-02-09 23:57:14 +09:00
parent 319b7fa69a
commit 3268781607
2 changed files with 27 additions and 19 deletions

View File

@ -168,11 +168,13 @@ public class SnappyFramedDecoder extends ByteToByteDecoder {
| in.readUnsignedByte() << 16
| in.readUnsignedByte() << 24;
if (validateChecksums) {
// TODO: Optimize me.
ByteBuf uncompressed = ctx.alloc().buffer();
snappy.decode(in, uncompressed, chunkLength);
snappy.decode(in.readSlice(chunkLength - 4), uncompressed, chunkLength);
validateChecksum(uncompressed, checksum);
out.writeBytes(uncompressed);
} else {
snappy.decode(in, out, chunkLength);
snappy.decode(in.readSlice(chunkLength - 4), out, chunkLength);
}
snappy.reset();
break;

View File

@ -18,17 +18,23 @@ package io.netty.handler.codec.compression;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedByteChannel;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class SnappyFramedDecoderTest {
private final EmbeddedByteChannel channel = new EmbeddedByteChannel(new SnappyFramedDecoder());
private EmbeddedByteChannel channel;
@Before
public void initChannel() {
channel = new EmbeddedByteChannel(new SnappyFramedDecoder());
}
@Test(expected = CompressionException.class)
public void testReservedUnskippableChunkTypeCausesError() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x03, 0x01, 0x00, 0x00
0x03, 0x01, 0x00, 0x00, 0x00
});
channel.writeInbound(in);
@ -37,7 +43,7 @@ public class SnappyFramedDecoderTest {
@Test(expected = CompressionException.class)
public void testInvalidStreamIdentifierLength() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x80, 0x05, 0x00, 'n', 'e', 't', 't', 'y'
-0x80, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
});
channel.writeInbound(in);
@ -46,7 +52,7 @@ public class SnappyFramedDecoderTest {
@Test(expected = CompressionException.class)
public void testInvalidStreamIdentifierValue() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x80, 0x06, 0x00, 's', 'n', 'e', 't', 't', 'y'
-0x80, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y'
});
channel.writeInbound(in);
@ -55,7 +61,7 @@ public class SnappyFramedDecoderTest {
@Test(expected = CompressionException.class)
public void testReservedSkippableBeforeStreamIdentifier() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x7f, 0x06, 0x00, 's', 'n', 'e', 't', 't', 'y'
-0x7f, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y'
});
channel.writeInbound(in);
@ -64,7 +70,7 @@ public class SnappyFramedDecoderTest {
@Test(expected = CompressionException.class)
public void testUncompressedDataBeforeStreamIdentifier() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x01, 0x05, 0x00, 'n', 'e', 't', 't', 'y'
0x01, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
});
channel.writeInbound(in);
@ -73,7 +79,7 @@ public class SnappyFramedDecoderTest {
@Test(expected = CompressionException.class)
public void testCompressedDataBeforeStreamIdentifier() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x00, 0x05, 0x00, 'n', 'e', 't', 't', 'y'
0x00, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
});
channel.writeInbound(in);
@ -82,21 +88,21 @@ public class SnappyFramedDecoderTest {
@Test
public void testReservedSkippableSkipsInput() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x80, 0x06, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
-0x7f, 0x05, 0x00, 'n', 'e', 't', 't', 'y'
-0x80, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
-0x7f, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
});
channel.writeInbound(in);
assertNull(channel.readInbound());
assertEquals(17, in.readerIndex());
assertFalse(in.isReadable());
}
@Test
public void testUncompressedDataAppendsToOut() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x80, 0x06, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
-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);
@ -108,11 +114,11 @@ public class SnappyFramedDecoderTest {
@Test
public void testCompressedDataDecodesAndAppendsToOut() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
-0x80, 0x06, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x05, // preamble length
0x04 << 2, // literal tag + length
0x6e, 0x65, 0x74, 0x74, 0x79 // "netty"
-0x80, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x00, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x05, // preamble length
0x04 << 2, // literal tag + length
0x6e, 0x65, 0x74, 0x74, 0x79 // "netty"
});
channel.writeInbound(in);