diff --git a/Tests/images/expected_to_read.jp2 b/Tests/images/expected_to_read.jp2 new file mode 100644 index 00000000000..d8029a0d3a0 Binary files /dev/null and b/Tests/images/expected_to_read.jp2 differ diff --git a/Tests/images/invalid_header_length.jp2 b/Tests/images/invalid_header_length.jp2 new file mode 100644 index 00000000000..c0c14f42160 Binary files /dev/null and b/Tests/images/invalid_header_length.jp2 differ diff --git a/Tests/images/not_enough_data.jp2 b/Tests/images/not_enough_data.jp2 new file mode 100644 index 00000000000..2d28bb5e96b Binary files /dev/null and b/Tests/images/not_enough_data.jp2 differ diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index 779d246e930..db5288880db 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -4,7 +4,7 @@ import pytest -from PIL import Image, ImageFile, Jpeg2KImagePlugin, features +from PIL import Image, ImageFile, Jpeg2KImagePlugin, UnidentifiedImageError, features from .helper import ( assert_image_equal, @@ -159,6 +159,20 @@ def test_load_dpi(): assert "dpi" not in im.info +def test_header_errors(): + for path in ( + "Tests/images/invalid_header_length.jp2", + "Tests/images/not_enough_data.jp2", + ): + with pytest.raises(UnidentifiedImageError): + with Image.open(path) as im: + pass + + with pytest.raises(OSError): + with Image.open("Tests/images/expected_to_read.jp2") as im: + pass + + def test_layers_type(tmp_path): outfile = str(tmp_path / "temp_layers.jp2") for quality_layers in [[100, 50, 10], (100, 50, 10), None]: