|
36 | 36 | import java.util.zip.Deflater; |
37 | 37 | import java.util.zip.GZIPOutputStream; |
38 | 38 |
|
| 39 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
39 | 40 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
40 | 41 | import static org.junit.jupiter.api.Assertions.assertEquals; |
41 | 42 | import static org.junit.jupiter.api.Assertions.assertFalse; |
@@ -140,6 +141,114 @@ public void testConcatenatedStreamsReadFullyWhenFragmented() throws IOException |
140 | 141 | } |
141 | 142 | } |
142 | 143 |
|
| 144 | + @Test |
| 145 | + public void testGZIPDecodeWithExtraField() throws Exception { |
| 146 | + byte[] data = "Hello, gzip FEXTRA world!".getBytes(CharsetUtil.UTF_8); |
| 147 | + byte[] extra = { 0x42, 0x43, 0x02, 0x00, (byte) 0x99, 0x00 }; // 6 arbitrary bytes |
| 148 | + byte[] gzipWithExtra = gzipWithExtraField(data, extra); |
| 149 | + |
| 150 | + // Sanity-check the crafted stream is a valid gzip by decoding it with the JDK itself. |
| 151 | + assertArrayEquals(data, jdkGunzip(gzipWithExtra)); |
| 152 | + |
| 153 | + // netty must decode it identically; before the FEXTRA fix the extra bytes were never |
| 154 | + // skipped, corrupting the deflate stream and throwing DecompressionException. |
| 155 | + EmbeddedChannel ch = new EmbeddedChannel(createDecoder(ZlibWrapper.GZIP)); |
| 156 | + try { |
| 157 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer(gzipWithExtra))); |
| 158 | + ByteBuf out = ch.readInbound(); |
| 159 | + assertEquals(new String(data, CharsetUtil.UTF_8), out.toString(CharsetUtil.UTF_8)); |
| 160 | + out.release(); |
| 161 | + } finally { |
| 162 | + assertFalse(ch.finish()); |
| 163 | + ch.close(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + @Test |
| 168 | + public void testConcatenatedGzipFirstStreamHasExtraField() throws Exception { |
| 169 | + // Regression guard: with concatenated streams, an FEXTRA field on the first stream must not |
| 170 | + // leak its XLEN into the second stream's header parsing. The xlen state has to be reset |
| 171 | + // between streams; otherwise the second stream (which has no extra field) would skip |
| 172 | + // xlen bytes that are actually deflate data and fail to decode. |
| 173 | + String firstText = "first stream"; |
| 174 | + String secondText = "second stream"; |
| 175 | + byte[] first = firstText.getBytes(CharsetUtil.UTF_8); |
| 176 | + byte[] second = secondText.getBytes(CharsetUtil.UTF_8); |
| 177 | + byte[] extra = { 0x42, 0x43, 0x02, 0x00, (byte) 0x99, 0x00 }; |
| 178 | + |
| 179 | + byte[] firstGz = gzipWithExtraField(first, extra); // first stream HAS an extra field |
| 180 | + byte[] secondGz = gzip(second); // second stream has none |
| 181 | + byte[] both = new byte[firstGz.length + secondGz.length]; |
| 182 | + System.arraycopy(firstGz, 0, both, 0, firstGz.length); |
| 183 | + System.arraycopy(secondGz, 0, both, firstGz.length, secondGz.length); |
| 184 | + |
| 185 | + EmbeddedChannel ch = new EmbeddedChannel(new JdkZlibDecoder(true, 0)); |
| 186 | + try { |
| 187 | + assertTrue(ch.writeInbound(Unpooled.copiedBuffer(both))); |
| 188 | + ByteArrayOutputStream decoded = new ByteArrayOutputStream(); |
| 189 | + ByteBuf msg; |
| 190 | + while ((msg = ch.readInbound()) != null) { |
| 191 | + msg.readBytes(decoded, msg.readableBytes()); |
| 192 | + msg.release(); |
| 193 | + } |
| 194 | + assertArrayEquals((firstText + secondText).getBytes(CharsetUtil.UTF_8), |
| 195 | + decoded.toByteArray()); |
| 196 | + decoded.close(); |
| 197 | + } finally { |
| 198 | + assertFalse(ch.finish()); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private static byte[] gzip(byte[] data) throws IOException { |
| 203 | + ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); |
| 204 | + GZIPOutputStream gzipOut = new GZIPOutputStream(bytesOut); |
| 205 | + gzipOut.write(data); |
| 206 | + gzipOut.close(); |
| 207 | + return bytesOut.toByteArray(); |
| 208 | + } |
| 209 | + |
| 210 | + private static byte[] gzipWithExtraField(byte[] data, byte[] extra) throws IOException { |
| 211 | + // GZIPOutputStream never emits an FEXTRA field, so build a standard gzip stream first ... |
| 212 | + byte[] standard = gzip(data); |
| 213 | + |
| 214 | + // ... then splice in an FEXTRA field by hand: set the FEXTRA flag in FLG and insert |
| 215 | + // XLEN (2 bytes, little-endian per RFC 1952) followed by the extra subfield, right after |
| 216 | + // the fixed 10-byte gzip header. |
| 217 | + ByteArrayOutputStream withExtra = new ByteArrayOutputStream(); |
| 218 | + try { |
| 219 | + byte[] header = Arrays.copyOfRange(standard, 0, 10); |
| 220 | + header[3] |= 0x04; // FLG.FEXTRA |
| 221 | + withExtra.write(header); |
| 222 | + withExtra.write(extra.length & 0xff); // XLEN low byte (little-endian) |
| 223 | + withExtra.write((extra.length >>> 8) & 0xff); // XLEN high byte |
| 224 | + withExtra.write(extra); |
| 225 | + withExtra.write(standard, 10, standard.length - 10); |
| 226 | + return withExtra.toByteArray(); |
| 227 | + } finally { |
| 228 | + withExtra.close(); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + private static byte[] jdkGunzip(byte[] gz) throws IOException { |
| 233 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 234 | + try { |
| 235 | + java.util.zip.GZIPInputStream in = |
| 236 | + new java.util.zip.GZIPInputStream(new java.io.ByteArrayInputStream(gz)); |
| 237 | + try { |
| 238 | + byte[] buf = new byte[256]; |
| 239 | + int n; |
| 240 | + while ((n = in.read(buf)) != -1) { |
| 241 | + out.write(buf, 0, n); |
| 242 | + } |
| 243 | + } finally { |
| 244 | + in.close(); |
| 245 | + } |
| 246 | + return out.toByteArray(); |
| 247 | + } finally { |
| 248 | + out.close(); |
| 249 | + } |
| 250 | + } |
| 251 | + |
143 | 252 | @Test |
144 | 253 | public void testDecodeWithHeaderFollowingFooter() throws Exception { |
145 | 254 | byte[] bytes = new byte[1024]; |
|