test: verify CHRNX image decoding
This commit is contained in:
1 file changed
+39
-1
@@ -3,6 +3,7 @@ import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
|
||||
@@ -25,6 +26,19 @@ def _opaque_coverage(alpha: Image.Image) -> float:
|
||||
return sum(alpha.histogram()[250:]) / (alpha.width * alpha.height)
|
||||
|
||||
|
||||
def _load_verified_image(path: Path) -> Image.Image:
|
||||
with Image.open(path) as image:
|
||||
image.verify()
|
||||
|
||||
image = Image.open(path)
|
||||
try:
|
||||
image.load()
|
||||
except Exception:
|
||||
image.close()
|
||||
raise
|
||||
return image
|
||||
|
||||
|
||||
def test_opaque_coverage_counts_near_opaque_alpha() -> None:
|
||||
alpha = Image.new("L", (4, 1))
|
||||
try:
|
||||
@@ -107,7 +121,7 @@ def test_background_asset_inventory_and_image_contract() -> None:
|
||||
violations: list[str] = []
|
||||
for name in BACKGROUND_ASSETS:
|
||||
path = ASSET_DIRECTORY / name
|
||||
with Image.open(path) as image:
|
||||
with _load_verified_image(path) as image:
|
||||
if image.format != "PNG":
|
||||
violations.append(f"{name}: expected PNG, got {image.format!r}")
|
||||
if image.mode not in {"RGB", "RGBA"}:
|
||||
@@ -130,6 +144,30 @@ def test_background_asset_inventory_and_image_contract() -> None:
|
||||
)
|
||||
|
||||
|
||||
def test_background_contract_rejects_corrupt_idat_data(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
path = tmp_path / BACKGROUND_ASSETS[0]
|
||||
image = Image.new("RGB", (1536, 960), (13, 17, 23))
|
||||
try:
|
||||
image.save(path)
|
||||
finally:
|
||||
image.close()
|
||||
|
||||
corrupt_png = bytearray(path.read_bytes())
|
||||
idat_type_offset = corrupt_png.index(b"IDAT")
|
||||
idat_length = int.from_bytes(
|
||||
corrupt_png[idat_type_offset - 4 : idat_type_offset], "big"
|
||||
)
|
||||
assert idat_length > 0
|
||||
corrupt_png[idat_type_offset + 4 + (idat_length // 2)] ^= 0x01
|
||||
path.write_bytes(corrupt_png)
|
||||
|
||||
monkeypatch.setattr(sys.modules[__name__], "ASSET_DIRECTORY", tmp_path)
|
||||
with pytest.raises((OSError, SyntaxError)):
|
||||
test_background_asset_inventory_and_image_contract()
|
||||
|
||||
|
||||
def test_gaming_half_body_preserves_original_and_has_prominent_visible_alpha() -> None:
|
||||
original = ASSET_DIRECTORY / "alter-ego-gaming.png"
|
||||
half_body = ASSET_DIRECTORY / "alter-ego-gaming-half-body.png"
|
||||
|
||||
Reference in new issue
Block a user