2017-02-17 18:07:14 +03:00
|
|
|
from helper import unittest, PillowTestCase, hopper
|
2014-05-27 13:40:52 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
2014-09-05 13:36:24 +04:00
|
|
|
TEST_FILE = "Tests/images/hopper.ppm"
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2014-06-23 12:22:25 +04:00
|
|
|
ORIGINAL_LIMIT = Image.MAX_IMAGE_PIXELS
|
|
|
|
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2014-06-23 12:12:41 +04:00
|
|
|
class TestDecompressionBomb(PillowTestCase):
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2014-06-23 12:22:25 +04:00
|
|
|
def tearDown(self):
|
|
|
|
Image.MAX_IMAGE_PIXELS = ORIGINAL_LIMIT
|
|
|
|
|
2014-06-23 12:12:41 +04:00
|
|
|
def test_no_warning_small_file(self):
|
|
|
|
# Implicit assert: no warning.
|
|
|
|
# A warning would cause a failure.
|
2014-09-05 13:36:24 +04:00
|
|
|
Image.open(TEST_FILE)
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2014-06-23 12:12:41 +04:00
|
|
|
def test_no_warning_no_limit(self):
|
|
|
|
# Arrange
|
|
|
|
# Turn limit off
|
|
|
|
Image.MAX_IMAGE_PIXELS = None
|
2017-06-03 07:04:54 +03:00
|
|
|
self.assertIsNone(Image.MAX_IMAGE_PIXELS)
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2014-06-23 12:12:41 +04:00
|
|
|
# Act / Assert
|
|
|
|
# Implicit assert: no warning.
|
|
|
|
# A warning would cause a failure.
|
2014-09-05 13:36:24 +04:00
|
|
|
Image.open(TEST_FILE)
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2014-06-23 12:12:41 +04:00
|
|
|
def test_warning(self):
|
2017-06-21 12:52:18 +03:00
|
|
|
# Set limit to trigger warning on the test file
|
2018-03-06 11:53:07 +03:00
|
|
|
Image.MAX_IMAGE_PIXELS = 128 * 128 - 1
|
2017-06-21 12:52:18 +03:00
|
|
|
self.assertEqual(Image.MAX_IMAGE_PIXELS, 128 * 128 - 1)
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2017-02-17 18:07:14 +03:00
|
|
|
self.assert_warning(Image.DecompressionBombWarning,
|
2017-09-01 14:05:40 +03:00
|
|
|
Image.open, TEST_FILE)
|
2017-02-17 18:07:14 +03:00
|
|
|
|
2017-06-21 12:52:18 +03:00
|
|
|
def test_exception(self):
|
|
|
|
# Set limit to trigger exception on the test file
|
2018-03-06 11:53:07 +03:00
|
|
|
Image.MAX_IMAGE_PIXELS = 64 * 128 - 1
|
2017-06-21 12:52:18 +03:00
|
|
|
self.assertEqual(Image.MAX_IMAGE_PIXELS, 64 * 128 - 1)
|
|
|
|
|
|
|
|
self.assertRaises(Image.DecompressionBombError,
|
|
|
|
lambda: Image.open(TEST_FILE))
|
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2017-02-17 18:07:14 +03:00
|
|
|
class TestDecompressionCrop(PillowTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.src = hopper()
|
2017-06-21 14:04:27 +03:00
|
|
|
Image.MAX_IMAGE_PIXELS = self.src.height * self.src.width * 4 - 1
|
2017-02-17 18:07:14 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
Image.MAX_IMAGE_PIXELS = ORIGINAL_LIMIT
|
|
|
|
|
|
|
|
def testEnlargeCrop(self):
|
|
|
|
# Crops can extend the extents, therefore we should have the
|
|
|
|
# same decompression bomb warnings on them.
|
|
|
|
box = (0, 0, self.src.width * 2, self.src.height * 2)
|
|
|
|
self.assert_warning(Image.DecompressionBombWarning,
|
2017-09-01 14:05:40 +03:00
|
|
|
self.src.crop, box)
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2017-06-21 12:52:18 +03:00
|
|
|
|
2014-06-23 12:12:41 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|