Merge pull request #2442 from hugovk/test-xvthumb

Test XVThumbImagePlugin for correctness
This commit is contained in:
wiredfool 2017-03-06 18:53:58 +00:00 committed by GitHub
commit 9f88a55fa3
4 changed files with 34 additions and 6 deletions

View File

@ -18,7 +18,7 @@
#
from . import Image, ImageFile, ImagePalette
from ._binary import o8
from ._binary import i8, o8
__version__ = "0.1"
@ -47,7 +47,7 @@ class XVThumbImageFile(ImageFile.ImageFile):
def _open(self):
# check magic
if self.fp.read(6) != _MAGIC:
if not _accept(self.fp.read(6)):
raise SyntaxError("not an XV thumbnail file")
# Skip to beginning of next line
@ -58,14 +58,14 @@ class XVThumbImageFile(ImageFile.ImageFile):
s = self.fp.readline()
if not s:
raise SyntaxError("Unexpected EOF reading XV thumbnail file")
if s[0] != b'#':
if i8(s[0]) != 35: # ie. when not a comment: '#'
break
# parse header line (already read)
s = s.strip().split()
self.mode = "P"
self.size = int(s[0:1]), int(s[1:2])
self.size = int(s[0]), int(s[1])
self.palette = ImagePalette.raw("RGB", PALETTE)
@ -74,6 +74,7 @@ class XVThumbImageFile(ImageFile.ImageFile):
self.fp.tell(), (self.mode, 0, 1)
)]
# --------------------------------------------------------------------
Image.register_open(XVThumbImageFile.format, XVThumbImageFile, _accept)

BIN
Tests/images/hopper.p7 Normal file

Binary file not shown.

View File

@ -0,0 +1,2 @@
P7 332
# Artificially edited file to cause unexpected EOF

View File

@ -1,13 +1,38 @@
from helper import unittest, PillowTestCase
from helper import hopper, unittest, PillowTestCase
from PIL import XVThumbImagePlugin
from PIL import Image, XVThumbImagePlugin
TEST_FILE = "Tests/images/hopper.p7"
class TestFileXVThumb(PillowTestCase):
def test_open(self):
# Act
im = Image.open(TEST_FILE)
# Assert
self.assertEqual(im.format, "XVThumb")
# Create a Hopper image with a similar XV palette
im_hopper = hopper().quantize(palette=im)
self.assert_image_similar(im, im_hopper, 9)
def test_unexpected_eof(self):
# Test unexpected EOF reading XV thumbnail file
# Arrange
bad_file = "Tests/images/hopper_bad.p7"
# Act / Assert
self.assertRaises(SyntaxError,
lambda:
XVThumbImagePlugin.XVThumbImageFile(bad_file))
def test_invalid_file(self):
# Arrange
invalid_file = "Tests/images/flower.jpg"
# Act / Assert
self.assertRaises(SyntaxError,
lambda:
XVThumbImagePlugin.XVThumbImageFile(invalid_file))