2014-07-07 21:03:50 +04:00
|
|
|
from helper import unittest, PillowTestCase
|
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
|
|
|
|
self.assertEqual(Image.MAX_IMAGE_PIXELS, None)
|
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):
|
|
|
|
# Arrange
|
|
|
|
# Set limit to a low, easily testable value
|
|
|
|
Image.MAX_IMAGE_PIXELS = 10
|
|
|
|
self.assertEqual(Image.MAX_IMAGE_PIXELS, 10)
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2014-06-23 12:12:41 +04:00
|
|
|
# Act / Assert
|
|
|
|
self.assert_warning(
|
|
|
|
Image.DecompressionBombWarning,
|
2014-09-05 13:36:24 +04:00
|
|
|
lambda: Image.open(TEST_FILE))
|
2014-05-27 13:40:52 +04:00
|
|
|
|
2014-06-23 12:12:41 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|