Skip to content

Commit

Permalink
SnappyFrameDecoderTest ByteBuf leak (netty#9854)
Browse files Browse the repository at this point in the history
Motivation:
SnappyFrameDecoderTest has a few tests which fail to close the EmbeddedChannel
and therefore may leak ByteBuf objects.

Modifications:
- Make sure EmbeddedChannel#finishAndReleaseAll() is called in all tests

Result:
No more leaks from SnappyFrameDecoderTest.
  • Loading branch information
Scottmitch authored Dec 7, 2019
1 parent d6638d5 commit 15b6ed9
Showing 1 changed file with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -31,14 +32,18 @@ public void initChannel() {
channel = new EmbeddedChannel(new SnappyFrameDecoder());
}

@After
public void tearDown() {
assertFalse(channel.finishAndReleaseAll());
}

@Test(expected = DecompressionException.class)
public void testReservedUnskippableChunkTypeCausesError() throws Exception {
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
0x03, 0x01, 0x00, 0x00, 0x00
});

assertFalse(channel.writeInbound(in));
assertFalse(channel.finish());
channel.writeInbound(in);
}

@Test(expected = DecompressionException.class)
Expand All @@ -47,8 +52,7 @@ public void testInvalidStreamIdentifierLength() throws Exception {
-0x80, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
});

assertFalse(channel.writeInbound(in));
assertFalse(channel.finish());
channel.writeInbound(in);
}

@Test(expected = DecompressionException.class)
Expand All @@ -57,8 +61,7 @@ public void testInvalidStreamIdentifierValue() throws Exception {
(byte) 0xff, 0x06, 0x00, 0x00, 's', 'n', 'e', 't', 't', 'y'
});

assertFalse(channel.writeInbound(in));
assertFalse(channel.finish());
channel.writeInbound(in);
}

@Test(expected = DecompressionException.class)
Expand All @@ -76,8 +79,7 @@ public void testUncompressedDataBeforeStreamIdentifier() throws Exception {
0x01, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
});

assertFalse(channel.writeInbound(in));
assertFalse(channel.finish());
channel.writeInbound(in);
}

@Test(expected = DecompressionException.class)
Expand All @@ -86,8 +88,7 @@ public void testCompressedDataBeforeStreamIdentifier() throws Exception {
0x00, 0x05, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
});

assertFalse(channel.writeInbound(in));
assertFalse(channel.finish());
channel.writeInbound(in);
}

@Test
Expand All @@ -101,7 +102,6 @@ public void testReservedSkippableSkipsInput() throws Exception {
assertNull(channel.readInbound());

assertFalse(in.isReadable());
assertFalse(channel.finish());
}

@Test
Expand All @@ -119,7 +119,6 @@ public void testUncompressedDataAppendsToOut() throws Exception {

expected.release();
actual.release();
assertFalse(channel.finish());
}

@Test
Expand All @@ -141,7 +140,6 @@ public void testCompressedDataDecodesAndAppendsToOut() throws Exception {

expected.release();
actual.release();
assertFalse(channel.finish());
}

// The following two tests differ in only the checksum provided for the literal
Expand All @@ -150,34 +148,38 @@ public void testCompressedDataDecodesAndAppendsToOut() throws Exception {
@Test(expected = DecompressionException.class)
public void testInvalidChecksumThrowsException() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true));

// checksum here is presented as 0
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 'n', 'e', 't', 't', 'y'
});

assertFalse(channel.writeInbound(in));
assertFalse(channel.finish());
try {
// checksum here is presented as 0
ByteBuf in = Unpooled.wrappedBuffer(new byte[]{
(byte) 0xff, 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);
} finally {
channel.finishAndReleaseAll();
}
}

@Test
public void testInvalidChecksumDoesNotThrowException() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(true));

// checksum here is presented as a282986f (little endian)
ByteBuf in = Unpooled.wrappedBuffer(new byte[] {
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y'
});

assertTrue(channel.writeInbound(in));
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' });
ByteBuf actual = channel.readInbound();
assertEquals(expected, actual);

expected.release();
actual.release();
assertFalse(channel.finish());
try {
// checksum here is presented as a282986f (little endian)
ByteBuf in = Unpooled.wrappedBuffer(new byte[]{
(byte) 0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59,
0x01, 0x09, 0x00, 0x00, 0x6f, -0x68, -0x7e, -0x5e, 'n', 'e', 't', 't', 'y'
});

assertTrue(channel.writeInbound(in));
ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'n', 'e', 't', 't', 'y' });
ByteBuf actual = channel.readInbound();
assertEquals(expected, actual);

expected.release();
actual.release();
} finally {
channel.finishAndReleaseAll();
}
}
}

0 comments on commit 15b6ed9

Please sign in to comment.