From d479e939b03cf37746647647917e028b1817e413 Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Thu, 10 Nov 2016 17:36:53 -0800 Subject: [PATCH] Buffer Leaks in Compression Tests Motivation: The unit tests for the compression encoders/decoders may write buffers to an EmbeddedChannel but then may not release buffer or close the channel after the test. This may result in buffer leaks. Modifications: - Call channel.finishAndReleaseAll() after each test Result: Fixes https://github.com/netty/netty/issues/6007 --- .../compression/AbstractDecoderTest.java | 9 ++++++++ .../compression/AbstractEncoderTest.java | 9 ++++++++ .../codec/compression/Bzip2DecoderTest.java | 21 +++++++++++++++---- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/codec/src/test/java/io/netty/handler/codec/compression/AbstractDecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/AbstractDecoderTest.java index 6267217d40..817c7c2f66 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/AbstractDecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/AbstractDecoderTest.java @@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.experimental.theories.DataPoints; @@ -63,6 +64,14 @@ public abstract class AbstractDecoderTest extends AbstractCompressionTest { @Before public abstract void initChannel(); + @After + public void destroyChannel() { + if (channel != null) { + channel.finishAndReleaseAll(); + channel = null; + } + } + @DataPoints("smallData") public static ByteBuf[] smallData() { ByteBuf heap = Unpooled.wrappedBuffer(compressedBytesSmall); diff --git a/codec/src/test/java/io/netty/handler/codec/compression/AbstractEncoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/AbstractEncoderTest.java index ba07dca118..632c9dd5c6 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/AbstractEncoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/AbstractEncoderTest.java @@ -19,6 +19,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.CompositeByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.embedded.EmbeddedChannel; +import org.junit.After; import org.junit.Before; import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.FromDataPoints; @@ -42,6 +43,14 @@ public abstract class AbstractEncoderTest extends AbstractCompressionTest { @Before public abstract void initChannel(); + @After + public void destroyChannel() { + if (channel != null) { + channel.finishAndReleaseAll(); + channel = null; + } + } + @DataPoints("smallData") public static ByteBuf[] smallData() { ByteBuf heap = Unpooled.wrappedBuffer(BYTES_SMALL); diff --git a/codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java b/codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java index 11382552ac..73fd2bd8b5 100644 --- a/codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java +++ b/codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java @@ -25,6 +25,7 @@ import java.io.ByteArrayOutputStream; import java.util.Arrays; import static io.netty.handler.codec.compression.Bzip2Constants.*; +import static org.junit.Assert.fail; public class Bzip2DecoderTest extends AbstractDecoderTest { @@ -43,6 +44,19 @@ public class Bzip2DecoderTest extends AbstractDecoderTest { channel = new EmbeddedChannel(new Bzip2Decoder()); } + private void writeInboundDestroyAndExpectDecompressionException(ByteBuf in) { + try { + channel.writeInbound(in); + } finally { + try { + destroyChannel(); + fail(); + } catch (DecompressionException ignored) { + // expected + } + } + } + @Test public void testUnexpectedStreamIdentifier() throws Exception { expected.expect(DecompressionException.class); @@ -50,8 +64,7 @@ public class Bzip2DecoderTest extends AbstractDecoderTest { ByteBuf in = Unpooled.buffer(); in.writeLong(1823080128301928729L); //random value - - channel.writeInbound(in); + writeInboundDestroyAndExpectDecompressionException(in); } @Test @@ -140,7 +153,7 @@ public class Bzip2DecoderTest extends AbstractDecoderTest { data[11] = 0x77; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + writeInboundDestroyAndExpectDecompressionException(in); } @Test @@ -152,7 +165,7 @@ public class Bzip2DecoderTest extends AbstractDecoderTest { data[14] = (byte) 0xFF; ByteBuf in = Unpooled.wrappedBuffer(data); - channel.writeInbound(in); + writeInboundDestroyAndExpectDecompressionException(in); } @Override