|
27 | 27 | import io.netty.handler.flow.FlowControlHandler; |
28 | 28 | import io.netty.util.ReferenceCountUtil; |
29 | 29 | import io.netty.util.test.DisabledForSlowLeakDetection; |
| 30 | +import com.aayushatharva.brotli4j.encoder.BrotliOutputStream; |
| 31 | +import org.junit.jupiter.api.Assumptions; |
30 | 32 | import org.junit.jupiter.api.Test; |
31 | 33 | import org.junit.jupiter.params.ParameterizedTest; |
32 | 34 | import org.junit.jupiter.params.provider.MethodSource; |
33 | 35 |
|
| 36 | +import java.io.ByteArrayOutputStream; |
34 | 37 | import java.util.ArrayList; |
| 38 | +import java.util.Arrays; |
35 | 39 | import java.util.List; |
36 | 40 | import java.util.concurrent.atomic.AtomicInteger; |
37 | 41 |
|
| 42 | +import static org.assertj.core.api.Assertions.assertThat; |
38 | 43 | import static org.junit.jupiter.api.Assertions.assertEquals; |
39 | 44 | import static org.junit.jupiter.api.Assertions.assertFalse; |
40 | 45 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -198,6 +203,74 @@ public void testZipBomb(String encoding) { |
198 | 203 | assertEquals((long) chunkSize * numberOfChunks, incomingHandler.total); |
199 | 204 | } |
200 | 205 |
|
| 206 | + @Test |
| 207 | + public void testBrotliDecodingHonorsMaxAllocationAsOutputCap() throws Exception { |
| 208 | + Assumptions.assumeTrue(Brotli.isAvailable(), |
| 209 | + "brotli4j native library not available on this platform"); |
| 210 | + |
| 211 | + // 128KB of moderately compressible bytes so the decompressed size |
| 212 | + // definitely exceeds BrotliDecoder's 64KB default output cap and any |
| 213 | + // small maxAllocation we might set below. |
| 214 | + byte[] payload = new byte[128 * 1024]; |
| 215 | + for (int i = 0; i < payload.length; i++) { |
| 216 | + payload[i] = (byte) (i % 251); |
| 217 | + } |
| 218 | + ByteArrayOutputStream compressedOut = new ByteArrayOutputStream(); |
| 219 | + BrotliOutputStream brotliOs = new BrotliOutputStream(compressedOut); |
| 220 | + brotliOs.write(payload); |
| 221 | + brotliOs.close(); |
| 222 | + byte[] compressed = compressedOut.toByteArray(); |
| 223 | + |
| 224 | + // Deliberately use a tiny maxAllocation. Before the fix this configured |
| 225 | + // BrotliDecoder.inputBufferSize = 64, effectively breaking the decoder |
| 226 | + // for realistic payloads; after the fix it configures outputBufferSize. |
| 227 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpContentDecompressor(64)); |
| 228 | + try { |
| 229 | + HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); |
| 230 | + response.headers().set(HttpHeaderNames.CONTENT_ENCODING, HttpHeaderValues.BR); |
| 231 | + response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED); |
| 232 | + assertTrue(channel.writeInbound(response)); |
| 233 | + assertTrue(channel.writeInbound(new DefaultHttpContent(Unpooled.wrappedBuffer(compressed)))); |
| 234 | + assertTrue(channel.writeInbound(LastHttpContent.EMPTY_LAST_CONTENT)); |
| 235 | + |
| 236 | + // Drain and concatenate every decompressed HttpContent chunk. |
| 237 | + byte[] decompressed = new byte[0]; |
| 238 | + int contentChunks = 0; |
| 239 | + Object msg; |
| 240 | + while ((msg = channel.readInbound()) != null) { |
| 241 | + try { |
| 242 | + if (msg instanceof HttpContent) { |
| 243 | + ByteBuf buf = ((HttpContent) msg).content(); |
| 244 | + if (buf.readableBytes() > 0) { |
| 245 | + contentChunks++; |
| 246 | + int len = decompressed.length; |
| 247 | + decompressed = Arrays.copyOf(decompressed, len + buf.readableBytes()); |
| 248 | + buf.readBytes(decompressed, len, buf.readableBytes()); |
| 249 | + } |
| 250 | + } |
| 251 | + } finally { |
| 252 | + ReferenceCountUtil.release(msg); |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + assertThat(decompressed) |
| 257 | + .as("decompressed bytes must match original payload") |
| 258 | + .isEqualTo(payload); |
| 259 | + |
| 260 | + // Regression signal: with maxAllocation=64 correctly routed to |
| 261 | + // BrotliDecoder.outputBufferSize, a 128KB payload must be forwarded |
| 262 | + // in many small chunks. Under the previous (buggy) wiring the same |
| 263 | + // maxAllocation would be applied to inputBufferSize while |
| 264 | + // outputBufferSize stayed at BrotliDecoder's 64KB default, so only |
| 265 | + // a handful of large chunks would be emitted. |
| 266 | + assertThat(contentChunks) |
| 267 | + .as("expected many small chunks when maxAllocation=64 caps output size") |
| 268 | + .isGreaterThan(100); |
| 269 | + } finally { |
| 270 | + channel.finishAndReleaseAll(); |
| 271 | + } |
| 272 | + } |
| 273 | + |
201 | 274 | private static final class ZipBombIncomingHandler extends ChannelInboundHandlerAdapter { |
202 | 275 | final int memoryLimit; |
203 | 276 | long total; |
|