Merge pull request #1 from radarhere/fix_jpeg_magic_number

Fix JPEG magic number
This commit is contained in:
Kirill Kuzminykh 2020-06-22 07:54:32 +03:00 committed by GitHub
commit cebaba1446
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,7 @@ import re
from io import BytesIO from io import BytesIO
import pytest import pytest
from PIL import ExifTags, Image, ImageFile, JpegImagePlugin from PIL import ExifTags, Image, ImageFile, JpegImagePlugin, UnidentifiedImageError
from .helper import ( from .helper import (
assert_image, assert_image,
@ -706,8 +706,8 @@ class TestFileJpeg:
with Image.open("Tests/images/icc-after-SOF.jpg") as im: with Image.open("Tests/images/icc-after-SOF.jpg") as im:
assert im.info["icc_profile"] == b"profile" assert im.info["icc_profile"] == b"profile"
def test_reading_not_whole_file_for_define_it_type(self): def test_jpeg_magic_number(self):
size = 1024 ** 2 size = 4097
buffer = BytesIO(b"\xFF" * size) # Many xFF bytes buffer = BytesIO(b"\xFF" * size) # Many xFF bytes
buffer.max_pos = 0 buffer.max_pos = 0
orig_read = buffer.read orig_read = buffer.read
@ -718,13 +718,11 @@ class TestFileJpeg:
return res return res
buffer.read = read buffer.read = read
with pytest.raises(OSError): with pytest.raises(UnidentifiedImageError):
Image.open(buffer) Image.open(buffer)
# Only small part of file has been read. # Assert the entire file has not been read
# The upper limit of max_pos (8Kb) was chosen experimentally assert 0 < buffer.max_pos < size
# and increased approximately twice.
assert 0 < buffer.max_pos < 8 * 1024
@pytest.mark.skipif(not is_win32(), reason="Windows only") @pytest.mark.skipif(not is_win32(), reason="Windows only")