Download backend/app/tests/test_image_guard.py from apoorvrajdev/image-captioning-api: direct link, hf CLI and curl.
- Browser
- Download file 6.28 kB
-
https://huggingface.co/spaces/apoorvrajdev/image-captioning-api/resolve/main/backend/app/tests/test_image_guard.py
- Command line
-
hf download hf://spaces/apoorvrajdev/image-captioning-api/backend/app/tests/test_image_guard.py
-
curl -L -o test_image_guard.py https://huggingface.co/spaces/apoorvrajdev/image-captioning-api/resolve/main/backend/app/tests/test_image_guard.py
6.28 kB
| """Tests for the upload dimension guard in ``app.utils.image``. | |
| ``check_image_dimensions`` reads the image header and must reject an image | |
| over the limits before ``bytes_to_tensor`` decodes it. Oversized inputs are | |
| header-only bytes (``image_bytes``), so nothing large is allocated, and | |
| TensorFlow is never imported here. | |
| """ | |
| from __future__ import annotations | |
| import struct | |
| import sys | |
| import pytest | |
| from app.tests.image_bytes import encode, jpeg_header, png_header | |
| from app.utils import image as image_module | |
| from app.utils.image import ( | |
| MAX_IMAGE_PIXELS, | |
| MAX_IMAGE_SIDE, | |
| ImageDecodeError, | |
| ImageTooLargeError, | |
| bytes_to_tensor, | |
| check_image_dimensions, | |
| ) | |
| LIMIT_MESSAGE = ( | |
| "Image dimensions exceed the limit of 16384 pixels per side and 40,000,000 pixels in total." | |
| ) | |
| def _bmp_with_width(width: int) -> bytes: | |
| data = bytearray(encode("BMP")) | |
| data[18:22] = struct.pack("<i", width) | |
| return bytes(data) | |
| def _png_with_bad_ihdr_crc() -> bytes: | |
| data = bytearray(png_header(32, 24)) | |
| data[29] ^= 0xFF # first byte of the IHDR checksum | |
| return bytes(data) | |
| def test_accepts_a_small_image(fmt: str) -> None: | |
| assert check_image_dimensions(encode(fmt, size=(32, 24))) == (32, 24) | |
| def test_accepts_dimensions_exactly_at_the_limits(header) -> None: | |
| assert MAX_IMAGE_PIXELS == 8_000 * 5_000 | |
| assert check_image_dimensions(header(8_000, 5_000)) == (8_000, 5_000) | |
| assert check_image_dimensions(header(MAX_IMAGE_SIDE, 1)) == (MAX_IMAGE_SIDE, 1) | |
| assert check_image_dimensions(header(1, MAX_IMAGE_SIDE)) == (1, MAX_IMAGE_SIDE) | |
| def test_rejects_a_pixel_count_over_the_limit(header) -> None: | |
| # Both sides are within MAX_IMAGE_SIDE; only the product is over. | |
| with pytest.raises(ImageTooLargeError): | |
| check_image_dimensions(header(8_000, 5_001)) | |
| def test_rejects_a_side_over_the_limit(header, size: tuple[int, int]) -> None: | |
| with pytest.raises(ImageTooLargeError): | |
| check_image_dimensions(header(*size)) | |
| def test_rejects_dimensions_past_pillows_own_bomb_ceiling() -> None: | |
| # 65535 x 65535 makes Image.open itself raise DecompressionBombError. | |
| with pytest.raises(ImageTooLargeError): | |
| check_image_dimensions(png_header(65_535, 65_535)) | |
| def test_limit_message_is_concise_and_names_only_the_limits() -> None: | |
| with pytest.raises(ImageTooLargeError) as info: | |
| check_image_dimensions(png_header(10_000, 5_000)) | |
| assert str(info.value) == LIMIT_MESSAGE | |
| def test_honours_explicit_limits() -> None: | |
| small = encode("PNG", size=(32, 24)) | |
| with pytest.raises(ImageTooLargeError): | |
| check_image_dimensions(small, max_side=31) | |
| with pytest.raises(ImageTooLargeError): | |
| check_image_dimensions(small, max_pixels=32 * 24 - 1) | |
| assert check_image_dimensions(small, max_side=32, max_pixels=32 * 24) == (32, 24) | |
| def test_malformed_or_invalid_headers_fail_with_a_client_error(data: bytes, error: type) -> None: | |
| with pytest.raises(ImageDecodeError) as info: | |
| check_image_dimensions(data) | |
| assert type(info.value) is error | |
| def test_rejects_non_positive_dimensions_from_the_parser( | |
| monkeypatch: pytest.MonkeyPatch, size: tuple[int, int] | |
| ) -> None: | |
| # Pillow already refuses these headers; the guard checks again on its own. | |
| class _Parsed: | |
| def __init__(self) -> None: | |
| self.size = size | |
| def __enter__(self) -> _Parsed: | |
| return self | |
| def __exit__(self, *exc_info: object) -> None: | |
| return None | |
| monkeypatch.setattr(image_module.Image, "open", lambda *args, **kwargs: _Parsed()) | |
| with pytest.raises(ImageDecodeError) as info: | |
| check_image_dimensions(b"stub") | |
| assert type(info.value) is ImageDecodeError | |
| def test_rejects_formats_outside_the_header_check(fmt: str) -> None: | |
| # A GIF is decoded at its largest frame's size, which its header doesn't | |
| # give, and opening a WebP allocates its canvas, so neither is parsed. | |
| # TF 2.15 can't decode WebP or TIFF anyway. | |
| with pytest.raises(ImageDecodeError) as info: | |
| check_image_dimensions(encode(fmt)) | |
| assert type(info.value) is ImageDecodeError | |
| def test_bytes_to_tensor_checks_dimensions_before_importing_tensorflow( | |
| monkeypatch: pytest.MonkeyPatch, | |
| ) -> None: | |
| # With TensorFlow unimportable, reaching the decoder raises ImportError. | |
| monkeypatch.setitem(sys.modules, "tensorflow", None) | |
| with pytest.raises(ImageTooLargeError): | |
| bytes_to_tensor(png_header(10_000, 5_000)) | |
| with pytest.raises(ImageDecodeError): | |
| bytes_to_tensor(b"not an image at all") | |
| with pytest.raises(ImportError): | |
| bytes_to_tensor(encode("PNG")) # within the limits: goes on to decode | |