Files
mtusi2026lab5/student-tests/worker/test_imaging.py
T
2026-10-26 19:40:34 +03:00

358 lines
20 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""lab5: обработка картинок worker (imaging.py, sandbox.py) — проверки внутри образа (там есть Pillow).
make imaging-test == docker compose --profile tools run --rm imaging-test
Уровень 3–4 (contracts/README.md, «Фоновая обработка»):
- геометрия: thumbnail вписан в 256×256 без увеличения, размеры вниз, минимум 1 px (320×200 -> 256×160);
crop — центральный квадрат 128×128 (маленькие увеличиваются); optimized — WebP в 1280×1280 без увеличения;
- EXIF-ориентация применяется (фото «на боку» в вариантах стоит правильно);
- прозрачность накладывается на белый в JPEG, а WebP её сохраняет;
- EXIF, GPS, XMP и ICC-профиль в варианты не попадают;
- 16-битные, палитровые, CMYK-картинки обрабатываются;
- одинаковый вход даёт одинаковые байты (повтор задания идемпотентен);
- лимиты worker: пиксели (до декодирования), память (RLIMIT_AS дочернего процесса), время (SIGKILL).
Замечание к MR: проверка структуры JPEG в API (images.py) сверяется здесь с настоящим декодером
worker (libjpeg-turbo в строгом режиме, та же версия, что в образе):
- случайные повреждения заголовков: если API файл пропустил, декодер может споткнуться только
о сжатые данные скана (их без декодирования не проверить), но не о заголовки; если декодер
файл целиком принял, API его тоже не отклоняет;
- полный перебор коэффициентов субдискретизации: API и TurboJPEG принимают одни и те же схемы.
"""
import hashlib
import io
import itertools
import os
import random
import struct
import tempfile
import time
import unittest
import warnings
import zlib
from pathlib import Path
import simplejpeg
from PIL import Image, ImageCms, ImageOps
from messenger import images, imaging, sandbox
FIXTURE = Path(__file__).resolve().parents[2] / "tests" / "fixtures" / "sample.png"
def _chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
def big_rgba_png(width, height):
"""Большой RGBA PNG без Pillow (нули сжимаются в килобайты), чтобы проверить лимиты памяти."""
compressor = zlib.compressobj(1)
row = b"\x00" + bytes(width * 4)
data = b"".join(compressor.compress(row) for _ in range(height)) + compressor.flush()
ihdr = struct.pack(">IIBBBBB", width, height, 8, 6, 0, 0, 0)
return b"\x89PNG\r\n\x1a\n" + _chunk(b"IHDR", ihdr) + _chunk(b"IDAT", data) + _chunk(b"IEND", b"")
# ---------- JPEG по сегментам (для сверки проверки API с декодером) ----------
def jpeg_parts(data):
"""JPEG -> [("seg", маркер, данные) | ("scan", заголовок SOS, сжатые данные) | ("eoi", хвост)]."""
i, parts = 2, []
while True:
marker = data[i + 1]
i += 2
if marker == 0xD9:
parts.append(("eoi", data[i:]))
return parts
length = int.from_bytes(data[i:i + 2], "big")
payload, i = data[i + 2:i + length], i + length
if marker != 0xDA:
parts.append(("seg", marker, payload))
continue
j = i
while data[j] != 0xFF or data[j + 1] == 0x00 or 0xD0 <= data[j + 1] <= 0xD7:
j += 1
parts.append(("scan", payload, data[i:j]))
i = j
def jpeg_join(parts):
out = bytearray(b"\xff\xd8")
for part in parts:
if part[0] == "seg":
out += bytes([0xFF, part[1]]) + struct.pack(">H", len(part[2]) + 2) + part[2]
elif part[0] == "scan":
out += b"\xff\xda" + struct.pack(">H", len(part[1]) + 2) + part[1] + part[2]
else:
out += b"\xff\xd9" + part[1]
return bytes(out)
def gradient_jpeg(mode="RGB", size=(48, 32), **options):
image = Image.new(mode, size)
pixels = image.load()
for y in range(size[1]):
for x in range(size[0]):
value = (x * 7 + y * 13) % 256
pixels[x, y] = value if mode == "L" else tuple((value + k * 60) % 256 for k in range(len(mode)))
out = io.BytesIO()
image.save(out, "JPEG", **options)
return out.getvalue()
def api_verdict(data):
"""Что ответит API: accept или причина 415 (images.inspect без HTTP)."""
try:
width, height = images._header_size(io.BytesIO(data), "JPEG")
images.check_jpeg(data, width, height)
return "accept"
except Exception as exc: # UnsupportedMediaType / Broken
return f"reject: {exc}"
def worker_verdict(data):
"""Что скажет worker: строгий libjpeg-turbo (как imaging._strict_jpeg_check) и Pillow."""
try:
_h, _w, colorspace, _sub = simplejpeg.decode_jpeg_header(data)
target = "CMYK" if colorspace in ("CMYK", "YCCK") else ("GRAY" if colorspace == "Gray" else "RGB")
simplejpeg.decode_jpeg(data, colorspace=target, strict=True, min_factor=8)
with warnings.catch_warnings():
warnings.simplefilter("ignore")
with Image.open(io.BytesIO(data)) as image:
image.load()
except Exception as exc:
return str(exc)
return "ok"
# Ошибки, которые libjpeg находит только внутри сжатых данных скана (без декодирования не проверить):
# неверный код Хаффмана, данные кончились раньше блоков, лишние байты в конце данных,
# RST не на границе интервала.
ENTROPY_ERRORS = ("bad Huffman code", "premature end of data segment", "extraneous bytes before marker",
"instead of RST")
class ImagingTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.dir = self.tmp.name
def tearDown(self):
self.tmp.cleanup()
def save(self, image, fmt, **kwargs):
path = os.path.join(self.dir, f"in-{len(os.listdir(self.dir))}.{fmt.lower()}")
image.save(path, fmt, **kwargs)
return path
def run_variants(self, path, content_type, params=imaging.Params()):
out = os.path.join(self.dir, f"out-{len(os.listdir(self.dir))}")
result = imaging.process(path, out, content_type, params)
return {v["name"]: v for v in result["variants"]}
def opened(self, variant):
img = Image.open(variant["path"])
img.load()
return img
def test_fixture_variants(self):
variants = self.run_variants(str(FIXTURE), "image/png")
expected = {"thumbnail": ("JPEG", (256, 160)), "crop": ("JPEG", (128, 128)), "optimized": ("WEBP", (320, 200))}
for name, (fmt, size) in expected.items():
img = self.opened(variants[name])
self.assertEqual((img.format, img.size), (fmt, size), name)
self.assertEqual((variants[name]["width"], variants[name]["height"]), size)
self.assertEqual(variants[name]["size_bytes"], os.path.getsize(variants[name]["path"]))
print("\n fixture 320×200 -> thumbnail JPEG 256×160, crop JPEG 128×128, optimized WebP 320×200")
def test_geometry(self):
cases = {(3000, 1000): ((256, 85), (1280, 426)), (1000, 3000): ((85, 256), (426, 1280)),
(10, 5): ((10, 5), (10, 5)), (5000, 10): ((256, 1), (1280, 2)),
(256, 256): ((256, 256), (256, 256)), (257, 256): ((256, 255), (257, 256)),
(2000, 1999): ((256, 255), (1280, 1279))}
for (w, h), (thumb, optimized) in cases.items():
with self.subTest(size=(w, h)):
variants = self.run_variants(self.save(Image.new("RGB", (w, h), (90, 120, 150)), "PNG"), "image/png")
self.assertEqual(self.opened(variants["thumbnail"]).size, thumb)
self.assertEqual(self.opened(variants["optimized"]).size, optimized)
self.assertEqual(self.opened(variants["crop"]).size, (128, 128)) # маленькие увеличиваются
def test_exif_orientation_is_applied(self):
img = Image.new("RGB", (400, 200), (0, 0, 255))
img.paste((255, 0, 0), (0, 0, 200, 200)) # левая половина красная
exif = Image.Exif()
exif[imaging.EXIF_ORIENTATION] = 6 # «повернуть на 90° по часовой» при показе
path = self.save(img, "JPEG", exif=exif.tobytes(), quality=95)
with Image.open(path) as source:
reference = ImageOps.exif_transpose(source) # как картинку покажет просмотрщик
variants = self.run_variants(path, "image/jpeg")
thumb = self.opened(variants["thumbnail"])
self.assertEqual((reference.size, thumb.size), ((200, 400), (128, 256)))
top, bottom = thumb.getpixel((64, 20)), thumb.getpixel((64, 236))
self.assertGreater(top[0], 200) # красная половина — сверху, как у reference
self.assertGreater(bottom[2], 200)
self.assertEqual(reference.getpixel((100, 20))[0] > 200, True)
self.assertEqual(self.opened(variants["optimized"]).size, (200, 400))
print("\n EXIF orientation 6: stored 400×200 -> variants 128×256 / 200×400, upright like a viewer shows it")
def test_alpha_is_white_in_jpeg_and_kept_in_webp(self):
transparent = Image.new("RGBA", (100, 80), (0, 0, 0, 0))
variants = self.run_variants(self.save(transparent, "PNG"), "image/png")
for name in ("thumbnail", "crop"):
pixel = self.opened(variants[name]).convert("RGB").getpixel((5, 5))
self.assertTrue(all(c >= 250 for c in pixel), f"{name}: {pixel}")
webp = self.opened(variants["optimized"])
self.assertEqual((webp.mode, webp.getpixel((5, 5))[3]), ("RGBA", 0))
palette = Image.new("P", (60, 40), 0)
palette.putpalette([0, 0, 0] * 256)
variants = self.run_variants(self.save(palette, "PNG", transparency=0), "image/png")
self.assertTrue(all(c >= 250 for c in self.opened(variants["thumbnail"]).convert("RGB").getpixel((3, 3))))
def test_metadata_is_stripped(self):
exif = Image.Exif()
exif[0x010F] = "Camera Maker"
exif[0x8825] = {1: "N", 2: (55.0, 45.0, 0.0)} # GPS-координаты
icc = ImageCms.ImageCmsProfile(ImageCms.createProfile("sRGB")).tobytes()
path = self.save(Image.new("RGB", (300, 200), (10, 200, 30)), "JPEG", exif=exif.tobytes(), icc_profile=icc)
with Image.open(path) as source:
self.assertIn("exif", source.info)
self.assertIn("icc_profile", source.info)
for name, variant in self.run_variants(path, "image/jpeg").items():
data = Path(variant["path"]).read_bytes()
img = self.opened(variant)
self.assertNotIn("exif", img.info, name)
self.assertNotIn("icc_profile", img.info, name)
self.assertNotIn(b"Exif\x00\x00", data, name)
self.assertNotIn(b"ICC_PROFILE", data, name)
self.assertNotIn(b"Camera Maker", data, name)
def test_modes(self):
sixteen = Image.new("I;16", (300, 200), 32768)
variants = self.run_variants(self.save(sixteen, "PNG"), "image/png")
self.assertAlmostEqual(self.opened(variants["thumbnail"]).getpixel((10, 10)), 128, delta=2)
cmyk = self.save(Image.new("CMYK", (300, 200), (0, 255, 255, 0)), "JPEG")
thumb = self.opened(self.run_variants(cmyk, "image/jpeg")["thumbnail"]).convert("RGB")
self.assertGreater(thumb.getpixel((10, 10))[0], 200) # красный остался красным
for mode in ("1", "L", "LA", "P", "RGB", "RGBA"):
with self.subTest(mode=mode):
variants = self.run_variants(self.save(Image.new(mode, (64, 48)), "PNG"), "image/png")
self.assertEqual(self.opened(variants["thumbnail"]).size, (64, 48))
def test_same_input_same_bytes(self):
first = self.run_variants(str(FIXTURE), "image/png")
second = self.run_variants(str(FIXTURE), "image/png")
self.assertEqual({k: v["sha256"] for k, v in first.items()}, {k: v["sha256"] for k, v in second.items()})
for variant in first.values():
self.assertEqual(hashlib.sha256(Path(variant["path"]).read_bytes()).hexdigest(), variant["sha256"])
def test_broken_jpeg_scan_is_invalid(self):
good = io.BytesIO()
Image.new("RGB", (64, 48), (200, 100, 50)).save(good, "JPEG")
data = good.getvalue()
sos = data.index(b"\xff\xda")
length = int.from_bytes(data[sos + 2:sos + 4], "big")
broken = data[:sos + 2 + length] + bytes((i * 37 + 11) % 255 for i in range(300)) + b"\xff\xd9"
path = os.path.join(self.dir, "broken.jpg")
Path(path).write_bytes(broken)
with Image.open(path) as lenient:
lenient.load() # Pillow такой файл молча «декодирует»...
with self.assertRaises(imaging.InvalidImage): # ...а строгая проверка libjpeg-turbo — нет
imaging.process(path, os.path.join(self.dir, "o"), "image/jpeg")
def test_jpeg_header_check_matches_strict_decoder(self):
bases = [gradient_jpeg(), gradient_jpeg(subsampling=0), gradient_jpeg(subsampling=1),
gradient_jpeg(progressive=True), gradient_jpeg(progressive=True, optimize=True),
gradient_jpeg(restart_marker_blocks=1), gradient_jpeg("L"), gradient_jpeg("CMYK"),
gradient_jpeg(size=(130, 70), restart_marker_rows=1),
gradient_jpeg(size=(64, 64), progressive=True, restart_marker_blocks=2)]
for data in bases:
self.assertEqual((api_verdict(data), worker_verdict(data)), ("accept", "ok"))
rnd = random.Random(20261026)
counts = {"both accept": 0, "both reject": 0, "accepted, entropy error in worker": 0}
for _ in range(800):
parts = jpeg_parts(rnd.choice(bases))
k = rnd.choice([n for n, part in enumerate(parts) if part[0] != "eoi"])
operation = rnd.randrange(6)
if operation <= 2: # байт в заголовке сегмента или скана
part = parts[k]
payload = bytearray(part[2] if part[0] == "seg" else part[1])
if payload:
position = rnd.randrange(len(payload))
payload[position] = rnd.choice([rnd.randrange(256), (payload[position] + 1) % 256,
payload[position] ^ (1 << rnd.randrange(8))])
parts[k] = ("seg", part[1], bytes(payload)) if part[0] == "seg" else ("scan", bytes(payload), part[2])
elif operation == 3: # сегмент или скан пропал
del parts[k]
elif operation == 4: # сегмент или скан повторился
parts.insert(k, parts[k])
else: # лишний или чужой маркер RST в сжатых данных
scans = [n for n, part in enumerate(parts) if part[0] == "scan"]
n = rnd.choice(scans)
entropy = bytearray(parts[n][2])
position = rnd.randrange(len(entropy))
entropy[position:position] = bytes([0xFF, 0xD0 + rnd.randrange(8)])
parts[n] = ("scan", parts[n][1], bytes(entropy))
data = jpeg_join(parts)
api, worker = api_verdict(data), worker_verdict(data)
with self.subTest(api=api, worker=worker):
if api == "accept":
# Пропущенный API файл не должен падать в worker на заголовках.
self.assertTrue(worker == "ok" or any(e in worker for e in ENTROPY_ERRORS))
else:
# Отклонённый API файл декодер тоже не принимает целиком.
self.assertNotEqual(worker, "ok")
if api == "accept":
counts["both accept" if worker == "ok" else "accepted, entropy error in worker"] += 1
else:
counts["both reject"] += 1
print(f"\n 800 random header mutations: {counts}")
def test_subsampling_rule_matches_turbojpeg(self):
factors = [(h, v) for h in range(1, 5) for v in range(1, 5)]
cases = [(gradient_jpeg("L"), [(f,) for f in factors]),
(gradient_jpeg(), list(itertools.product(factors, repeat=3))),
(gradient_jpeg("CMYK"), [(y, c, c, k) for y in factors for c in factors for k in factors])]
compared = 0
for data, combinations in cases:
parts = jpeg_parts(data)
sof = next(n for n, part in enumerate(parts) if part[0] == "seg" and part[1] == 0xC0)
for combination in combinations:
payload = bytearray(parts[sof][2])
for k, (h, v) in enumerate(combination):
payload[7 + 3 * k] = h * 16 + v
parts[sof] = ("seg", 0xC0, bytes(payload))
try:
simplejpeg.decode_jpeg_header(jpeg_join(parts))
decoder = True
except (ValueError, KeyError): # KeyError: схема, которой нет в simplejpeg (4:4:1)
decoder = False
self.assertEqual(images._subsampling_supported(list(combination)), decoder, combination)
compared += 1
print(f"\n {compared} sampling-factor combinations: API and TurboJPEG agree on every one")
def test_limits_in_child_process(self):
path = os.path.join(self.dir, "big.png")
Path(path).write_bytes(big_rgba_png(6000, 6000)) # 36 млн пикселей RGBA
params = imaging.Params()
started = time.monotonic()
result = sandbox.run(path, os.path.join(self.dir, "ok"), "image/png", params, timeout=60, memory_limit_mb=640)
normal = time.monotonic() - started
self.assertEqual({v["name"] for v in result["variants"]}, {"thumbnail", "crop", "optimized"})
with self.assertRaises(imaging.ImageTooLarge): # память: RLIMIT_AS 200 МиБ
sandbox.run(path, os.path.join(self.dir, "m"), "image/png", params, timeout=60, memory_limit_mb=200)
started = time.monotonic()
with self.assertRaises(sandbox.ProcessingTimeout): # время: SIGKILL дочернему процессу
sandbox.run(path, os.path.join(self.dir, "t"), "image/png", params, timeout=0.2, memory_limit_mb=640)
killed_after = time.monotonic() - started
self.assertLess(killed_after, 2)
with self.assertRaises(imaging.ImageTooLarge): # пиксели: до декодирования
imaging.process(str(FIXTURE), os.path.join(self.dir, "p"), "image/png", imaging.Params(max_pixels=1000))
direct = imaging.process(str(FIXTURE), os.path.join(self.dir, "d"), "image/png")
child = sandbox.run(str(FIXTURE), os.path.join(self.dir, "c"), "image/png", params, 60, 640)
self.assertEqual([v["sha256"] for v in direct["variants"]], [v["sha256"] for v in child["variants"]])
print(f"\n 36 Mpx RGBA in a child process: ok in {normal:.2f} s with 640 MiB; MemoryError -> image_too_large "
f"with 200 MiB; timeout 0.2 s -> killed after {killed_after:.2f} s; 1000 px limit -> image_too_large")
if __name__ == "__main__":
unittest.main(verbosity=2)