|
25 | 25 | import io.netty.util.ReferenceCountUtil; |
26 | 26 | import io.netty.util.internal.EmptyArrays; |
27 | 27 | import org.junit.jupiter.api.Test; |
| 28 | +import org.assertj.core.api.ThrowableAssert; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.CsvSource; |
| 31 | +import org.junit.jupiter.params.provider.ValueSource; |
28 | 32 |
|
29 | 33 | import java.io.ByteArrayOutputStream; |
30 | 34 | import java.io.IOException; |
31 | 35 | import java.io.OutputStream; |
| 36 | +import java.util.Arrays; |
32 | 37 | import java.util.Random; |
33 | 38 | import java.util.concurrent.ThreadLocalRandom; |
34 | 39 | import java.util.zip.DeflaterOutputStream; |
35 | 40 | import java.util.zip.GZIPInputStream; |
36 | 41 | import java.util.zip.GZIPOutputStream; |
37 | 42 |
|
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
38 | 45 | import static org.junit.jupiter.api.Assertions.assertEquals; |
39 | 46 | import static org.junit.jupiter.api.Assertions.assertFalse; |
40 | 47 | import static org.junit.jupiter.api.Assertions.assertNull; |
@@ -408,6 +415,83 @@ public void testMaxAllocation() throws Exception { |
408 | 415 | assertFalse(chDecoder.finish()); |
409 | 416 | } |
410 | 417 |
|
| 418 | + @ParameterizedTest |
| 419 | + @CsvSource({ "ZLIB, 2", "ZLIB, 64", "GZIP, 64", "NONE, 64", "ZLIB, 512", "GZIP, 512", "NONE, 512" }) |
| 420 | + public void testMaxAllocationWithInputSplitAcrossReads(ZlibWrapper wrapper, int maxAllocation) throws Exception { |
| 421 | + // The whole output is smaller than maxAllocation, so no read can fill the buffer. |
| 422 | + byte[] data = Arrays.copyOf(BYTES_LARGE2, Math.min(maxAllocation - 1, BYTES_LARGE2.length)); |
| 423 | + byte[] compressed = compress(wrapper, data); |
| 424 | + |
| 425 | + assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, compressed.length)) |
| 426 | + .isEqualTo(data); |
| 427 | + assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, compressed.length / 2 + 1)) |
| 428 | + .isEqualTo(data); |
| 429 | + assertThat(decodeInChunks(createDecoder(wrapper, maxAllocation), compressed, 1)) |
| 430 | + .isEqualTo(data); |
| 431 | + } |
| 432 | + |
| 433 | + @ParameterizedTest |
| 434 | + @ValueSource(ints = { 600, 1000, 1024 }) |
| 435 | + public void testMaxAllocationWithIncompressibleInputInOneRead(int size) throws Exception { |
| 436 | + // Random bytes do not compress, so the input is about as long as the output. |
| 437 | + byte[] data = Arrays.copyOf(BYTES_LARGE, size); |
| 438 | + byte[] compressed = compress(ZlibWrapper.ZLIB, data); |
| 439 | + |
| 440 | + assertThat(decodeInChunks(createDecoder(ZlibWrapper.ZLIB, 1024), compressed, compressed.length)) |
| 441 | + .isEqualTo(data); |
| 442 | + } |
| 443 | + |
| 444 | + @ParameterizedTest |
| 445 | + @CsvSource({ "ZLIB, 64", "GZIP, 64", "NONE, 64", "ZLIB, 512" }) |
| 446 | + public void testMaxAllocationStillLimitsOutputOfOneRead(ZlibWrapper wrapper, int maxAllocation) throws Exception { |
| 447 | + // A small input whose output is far larger than maxAllocation is still rejected. |
| 448 | + final byte[] compressed = compress(wrapper, new byte[65536]); |
| 449 | + final EmbeddedChannel channel = new EmbeddedChannel(createDecoder(wrapper, maxAllocation)); |
| 450 | + |
| 451 | + assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { |
| 452 | + @Override |
| 453 | + public void call() throws Throwable { |
| 454 | + channel.writeInbound(Unpooled.wrappedBuffer(compressed)); |
| 455 | + } |
| 456 | + }) |
| 457 | + .isInstanceOf(DecompressionException.class) |
| 458 | + .hasMessageStartingWith("Decompression buffer has reached maximum size"); |
| 459 | + channel.finishAndReleaseAll(); |
| 460 | + } |
| 461 | + |
| 462 | + private byte[] compress(ZlibWrapper wrapper, byte[] data) throws Exception { |
| 463 | + EmbeddedChannel channel = new EmbeddedChannel(createEncoder(wrapper)); |
| 464 | + channel.writeOutbound(Unpooled.wrappedBuffer(data)); |
| 465 | + channel.finish(); |
| 466 | + return readAll(channel, false); |
| 467 | + } |
| 468 | + |
| 469 | + private static byte[] decodeInChunks(ZlibDecoder decoder, byte[] compressed, int chunkSize) throws Exception { |
| 470 | + EmbeddedChannel channel = new EmbeddedChannel(decoder); |
| 471 | + for (int offset = 0; offset < compressed.length; offset += chunkSize) { |
| 472 | + int length = Math.min(chunkSize, compressed.length - offset); |
| 473 | + channel.writeInbound(Unpooled.wrappedBuffer(compressed, offset, length)); |
| 474 | + } |
| 475 | + channel.finish(); |
| 476 | + return readAll(channel, true); |
| 477 | + } |
| 478 | + |
| 479 | + private static byte[] readAll(EmbeddedChannel channel, boolean inbound) { |
| 480 | + ByteBuf all = Unpooled.buffer(); |
| 481 | + for (;;) { |
| 482 | + ByteBuf buf = inbound ? channel.<ByteBuf>readInbound() : channel.<ByteBuf>readOutbound(); |
| 483 | + if (buf == null) { |
| 484 | + break; |
| 485 | + } |
| 486 | + all.writeBytes(buf); |
| 487 | + buf.release(); |
| 488 | + } |
| 489 | + byte[] bytes = new byte[all.readableBytes()]; |
| 490 | + all.readBytes(bytes); |
| 491 | + all.release(); |
| 492 | + return bytes; |
| 493 | + } |
| 494 | + |
411 | 495 | private static byte[] gzip(byte[] bytes) throws IOException { |
412 | 496 | ByteArrayOutputStream out = new ByteArrayOutputStream(); |
413 | 497 | GZIPOutputStream stream = new GZIPOutputStream(out); |
|