73 lines
2.6 KiB
Python
73 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Cross-check the student test corpus against independent decoders.
|
|
|
|
Corrupted inputs (student-tests/imagegen.py) must NOT decode, valid ones must.
|
|
PNG → Pillow full load(); JPEG → libjpeg-turbo `djpeg -strict` (what the API runs).
|
|
Run in a throwaway container (needs Pillow + libjpeg-turbo-progs):
|
|
|
|
docker run --rm -v "$PWD:/w" -w /w python:3.12.8-slim-bookworm sh -c \
|
|
"apt-get update -qq && apt-get install -y -qq libjpeg-turbo-progs >/dev/null; \
|
|
pip install -q Pillow==11.1.0; python scripts/decoder_crosscheck.py"
|
|
"""
|
|
|
|
import io
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "student-tests"))
|
|
import imagegen # noqa: E402
|
|
from PIL import Image # noqa: E402
|
|
|
|
# PNG spec errors (PLTE length/placement, unknown critical chunk) that Pillow tolerates;
|
|
# libpng and the API reject them.
|
|
SPEC_ONLY = {"plte_not_multiple_of_3", "plte_too_long_for_1bit", "plte_in_grayscale", "unknown_critical_chunk"}
|
|
|
|
|
|
def pillow_ok(data: bytes) -> bool:
|
|
try:
|
|
img = Image.open(io.BytesIO(data))
|
|
img.load()
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
|
|
def djpeg_ok(data: bytes) -> bool:
|
|
with tempfile.NamedTemporaryFile(delete=False) as f:
|
|
f.write(data)
|
|
try:
|
|
cmd = ["djpeg", "-strict", "-scale", "1/8", "-outfile", os.devnull, f.name]
|
|
return subprocess.run(cmd, capture_output=True).returncode == 0
|
|
finally:
|
|
os.unlink(f.name)
|
|
|
|
|
|
def main() -> int:
|
|
mismatches = 0
|
|
for name, data in imagegen.corrupted_pngs().items():
|
|
ok = pillow_ok(data)
|
|
note = " (PNG spec violation tolerated by Pillow)" if ok and name in SPEC_ONLY else ""
|
|
mismatches += ok and name not in SPEC_ONLY
|
|
print(f"corrupt png {name:32} pillow_decodes={ok}{note}")
|
|
for name, data in imagegen.corrupted_jpegs().items():
|
|
ok = djpeg_ok(data)
|
|
mismatches += ok
|
|
print(f"corrupt jpeg {name:32} djpeg_strict_decodes={ok} pillow_decodes={pillow_ok(data)}")
|
|
for interlace in (0, 1):
|
|
for size in ((1, 1), (9, 7), (33, 20)):
|
|
ok = pillow_ok(imagegen.all_filters_png(*size, interlace=interlace))
|
|
mismatches += not ok
|
|
print(f"valid png all_filters interlace={interlace} {size} pillow_decodes={ok}")
|
|
for name in ("sample.jpg", "progressive.jpg", "gray.jpg"):
|
|
ok = djpeg_ok(imagegen.fixture(name))
|
|
mismatches += not ok
|
|
print(f"valid jpeg {name:32} djpeg_strict_decodes={ok}")
|
|
print("PASS" if mismatches == 0 else f"FAIL mismatches={mismatches}")
|
|
return 1 if mismatches else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|