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