summaryrefslogtreecommitdiff
path: root/tests/files
diff options
context:
space:
mode:
authorsteve <steve.kossouho@gmail.com>2015-03-27 20:54:14 +0100
committerTim Graham <timograham@gmail.com>2015-04-07 19:51:19 -0400
commit2fc19b92387d4248c715edfbf38658238978abb6 (patch)
tree3dcbd2ce71dc3dbd9e57c870b092e2e4873cf782 /tests/files
parent800240cb1dda2c4ff7182df3566d744c6c377bf7 (diff)
downloaddjango-2fc19b92387d4248c715edfbf38658238978abb6.tar.gz
Fixed #24544 -- Fixed get_image_dimensions() on image buffers that Pillow fails to parse.
Thanks Steve Kossouho for the report and original patch.
Diffstat (limited to 'tests/files')
-rw-r--r--tests/files/tests.py21
1 files changed, 19 insertions, 2 deletions
diff --git a/tests/files/tests.py b/tests/files/tests.py
index d5d952d1e0..13560e509c 100644
--- a/tests/files/tests.py
+++ b/tests/files/tests.py
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
import gzip
import os
+import struct
import tempfile
import unittest
import zlib
@@ -13,6 +14,7 @@ from django.core.files.base import ContentFile
from django.core.files.move import file_move_safe
from django.core.files.temp import NamedTemporaryFile
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile
+from django.test import mock
from django.utils import six
from django.utils._os import upath
@@ -239,8 +241,9 @@ class InconsistentGetImageDimensionsBug(unittest.TestCase):
self.assertEqual(size, Image.open(fh).size)
-class GetImageDimensionsOnInvalidImages(unittest.TestCase):
- @unittest.skipUnless(Image, "Pillow not installed")
+@unittest.skipUnless(Image, "Pillow not installed")
+class GetImageDimensionsTests(unittest.TestCase):
+
def test_invalid_image(self):
"""
get_image_dimensions() should return (None, None) for the dimensions of
@@ -254,6 +257,20 @@ class GetImageDimensionsOnInvalidImages(unittest.TestCase):
size = images.get_image_dimensions(fh)
self.assertEqual(size, (None, None))
+ def test_valid_image(self):
+ """
+ get_image_dimensions() should catch struct.error while feeding the PIL
+ Image parser (#24544).
+
+ Emulates the Parser feed error. Since the error is raised on every feed
+ attempt, the resulting image size should be invalid: (None, None).
+ """
+ img_path = os.path.join(os.path.dirname(upath(__file__)), "test.png")
+ with mock.patch('PIL.ImageFile.Parser.feed', side_effect=struct.error):
+ with open(img_path, 'rb') as fh:
+ size = images.get_image_dimensions(fh)
+ self.assertEqual(size, (None, None))
+
class FileMoveSafeTests(unittest.TestCase):
def test_file_move_overwrite(self):