|
30 | 30 | import sys
|
31 | 31 | import tempfile
|
32 | 32 | import zlib
|
| 33 | +from gzip import FCOMMENT, FEXTRA, FHCRC, FNAME, FTEXT # type: ignore |
33 | 34 | from pathlib import Path
|
34 | 35 |
|
35 |
| -from isal import igzip |
| 36 | +from isal import igzip, isal_zlib |
36 | 37 |
|
37 | 38 | import pytest
|
38 | 39 |
|
@@ -214,3 +215,60 @@ def test_decompress_unknown_compression_method():
|
214 | 215 |
|
215 | 216 | def test_decompress_empty():
|
216 | 217 | assert igzip.decompress(b"") == b""
|
| 218 | + |
| 219 | + |
| 220 | +def headers(): |
| 221 | + magic = b"\x1f\x8b" |
| 222 | + method = b"\x08" |
| 223 | + mtime = b"\x00\x00\x00\x00" |
| 224 | + xfl = b"\x00" |
| 225 | + os = b"\xff" |
| 226 | + common_hdr_start = magic + method |
| 227 | + common_hdr_end = mtime + xfl + os |
| 228 | + xtra = b"METADATA" |
| 229 | + xlen = len(xtra) |
| 230 | + fname = b"my_data.tar" |
| 231 | + fcomment = b"I wrote this header with my bare hands" |
| 232 | + yield (common_hdr_start + FEXTRA.to_bytes(1, "little") + |
| 233 | + common_hdr_end + xlen.to_bytes(2, "little") + xtra) |
| 234 | + yield (common_hdr_start + FNAME.to_bytes(1, "little") + |
| 235 | + common_hdr_end + fname + b"\x00") |
| 236 | + yield (common_hdr_start + FCOMMENT.to_bytes(1, "little") + |
| 237 | + common_hdr_end + fcomment + b"\x00") |
| 238 | + flag = FHCRC.to_bytes(1, "little") |
| 239 | + header = common_hdr_start + flag + common_hdr_end |
| 240 | + crc = zlib.crc32(header) & 0xFFFF |
| 241 | + yield(header + crc.to_bytes(2, "little")) |
| 242 | + flag_bits = FTEXT | FEXTRA | FNAME | FCOMMENT | FHCRC |
| 243 | + flag = flag_bits.to_bytes(1, "little") |
| 244 | + header = (common_hdr_start + flag + common_hdr_end + |
| 245 | + xlen.to_bytes(2, "little") + xtra + fname + b"\x00" + |
| 246 | + fcomment + b"\x00") |
| 247 | + crc = zlib.crc32(header) & 0xFFFF |
| 248 | + yield header + crc.to_bytes(2, "little") |
| 249 | + |
| 250 | + |
| 251 | +@pytest.mark.parametrize("header", list(headers())) |
| 252 | +def test_gzip_header_end(header): |
| 253 | + assert igzip._gzip_header_end(header) == len(header) |
| 254 | + |
| 255 | + |
| 256 | +def test_header_too_short(): |
| 257 | + with pytest.raises(igzip.BadGzipFile): |
| 258 | + gzip.decompress(b"00") |
| 259 | + |
| 260 | + |
| 261 | +def test_header_corrupt(): |
| 262 | + header = b"\x1f\x8b\x08\x02\x00\x00\x00\x00\x00\xff" |
| 263 | + # Create corrupt checksum by using wrong seed. |
| 264 | + crc = zlib.crc32(header, 50) & 0xFFFF |
| 265 | + true_crc = zlib.crc32(header) & 0xFFFF |
| 266 | + header += crc.to_bytes(2, "little") |
| 267 | + |
| 268 | + data = isal_zlib.compress(b"", wbits=-15) |
| 269 | + trailer = b"\x00" * 8 |
| 270 | + compressed = header + data + trailer |
| 271 | + with pytest.raises(igzip.BadGzipFile) as error: |
| 272 | + igzip.decompress(compressed) |
| 273 | + error.match(f"Corrupted header. " |
| 274 | + f"Checksums do not match: {true_crc} != {crc}") |
0 commit comments