Pillow/Tests/test_decompression_bomb.py

46 lines
1.1 KiB
Python
Raw Normal View History

from helper import unittest, PillowTestCase, tearDownModule
2014-05-27 13:40:52 +04:00
from PIL import Image
2014-06-23 22:38:03 +04:00
test_file = "Tests/images/lena.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
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
def test_no_warning_small_file(self):
# Implicit assert: no warning.
# A warning would cause a failure.
Image.open(test_file)
2014-05-27 13:40:52 +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
# Act / Assert
# Implicit assert: no warning.
# A warning would cause a failure.
Image.open(test_file)
2014-05-27 13:40:52 +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
# Act / Assert
self.assert_warning(
Image.DecompressionBombWarning,
lambda: Image.open(test_file))
2014-05-27 13:40:52 +04:00
if __name__ == '__main__':
unittest.main()
2014-05-27 13:40:52 +04:00
# End of file