Fix buffer leak in test which was introduced while implement ZLIB_OR_NONE support. Related to [#2269]

This commit is contained in:
Norman Maurer 2014-03-06 20:03:40 +01:00
parent 2b47058644
commit 835c446f5f
2 changed files with 86 additions and 68 deletions

View File

@ -385,7 +385,7 @@ public class JdkZlibDecoder extends ZlibDecoder {
* You can lookup the details in the ZLIB RFC: * You can lookup the details in the ZLIB RFC:
* <a href="http://tools.ietf.org/html/rfc1950#section-2.2">RFC 1950</a>. * <a href="http://tools.ietf.org/html/rfc1950#section-2.2">RFC 1950</a>.
*/ */
private boolean looksLikeZlib(short cmf_flg) { private static boolean looksLikeZlib(short cmf_flg) {
return (cmf_flg & 0x7800) == 0x7800 && return (cmf_flg & 0x7800) == 0x7800 &&
cmf_flg % 31 == 0; cmf_flg % 31 == 0;
} }

View File

@ -48,6 +48,7 @@ public abstract class ZlibTest {
ByteBuf deflatedData = Unpooled.wrappedBuffer(gzip("message")); ByteBuf deflatedData = Unpooled.wrappedBuffer(gzip("message"));
EmbeddedChannel chDecoderGZip = new EmbeddedChannel(createDecoder(ZlibWrapper.GZIP)); EmbeddedChannel chDecoderGZip = new EmbeddedChannel(createDecoder(ZlibWrapper.GZIP));
try {
chDecoderGZip.writeInbound(deflatedData.copy()); chDecoderGZip.writeInbound(deflatedData.copy());
assertTrue(chDecoderGZip.finish()); assertTrue(chDecoderGZip.finish());
ByteBuf buf = (ByteBuf) chDecoderGZip.readInbound(); ByteBuf buf = (ByteBuf) chDecoderGZip.readInbound();
@ -56,15 +57,20 @@ public abstract class ZlibTest {
data.release(); data.release();
deflatedData.release(); deflatedData.release();
buf.release(); buf.release();
} finally {
// close channel to prevent any leak even on exception
chDecoderGZip.close();
}
} }
private void testCompress0(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper, ByteBuf data) throws Exception { private void testCompress0(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper, ByteBuf data) throws Exception {
EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper)); EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));
EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
try {
chEncoder.writeOutbound(data.copy()); chEncoder.writeOutbound(data.copy());
chEncoder.flush(); chEncoder.flush();
EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
for (;;) { for (;;) {
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
if (deflatedData == null) { if (deflatedData == null) {
@ -104,15 +110,20 @@ public abstract class ZlibTest {
assertFalse(chDecoderZlib.finish()); assertFalse(chDecoderZlib.finish());
data.release(); data.release();
} finally {
// close channels in all cases to guard against leak when exception was thrown
chEncoder.close();
chDecoderZlib.close();
}
} }
private void testCompressNone(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper) throws Exception { private void testCompressNone(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper) throws Exception {
EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper)); EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));
EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
try {
// Closing an encoder channel without writing anything should generate both header and footer. // Closing an encoder channel without writing anything should generate both header and footer.
assertTrue(chEncoder.finish()); assertTrue(chEncoder.finish());
EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
for (;;) { for (;;) {
ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound(); ByteBuf deflatedData = (ByteBuf) chEncoder.readOutbound();
if (deflatedData == null) { if (deflatedData == null) {
@ -122,6 +133,7 @@ public abstract class ZlibTest {
} }
// Decoder should not generate anything at all. // Decoder should not generate anything at all.
boolean decoded = false;
for (;;) { for (;;) {
ByteBuf buf = (ByteBuf) chDecoderZlib.readInbound(); ByteBuf buf = (ByteBuf) chDecoderZlib.readInbound();
if (buf == null) { if (buf == null) {
@ -129,10 +141,16 @@ public abstract class ZlibTest {
} }
buf.release(); buf.release();
fail("should decode nothing"); decoded = true;
} }
assertFalse("should decode nothing", decoded);
assertFalse(chDecoderZlib.finish()); assertFalse(chDecoderZlib.finish());
} finally {
// close channels in all cases to guard against leak when exception was thrown
chEncoder.close();
chDecoderZlib.close();
}
} }
private void testCompressSmall(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper) throws Exception { private void testCompressSmall(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper) throws Exception {