1
0
mirror of synced 2024-12-22 04:39:58 +01:00

Add a test for PNG files

This commit is contained in:
jvoisin 2018-03-20 23:35:02 +01:00
parent d48dfc8e21
commit 7ad9ff08ad
3 changed files with 36 additions and 9 deletions

View File

@ -7,12 +7,21 @@ from . import abstract
class PNGParser(abstract.AbstractParser):
mimetypes = {'image/png', }
meta_list = set()
meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate',
"FileInodeChangeDate", 'FilePermissions', 'FileType',
'FileTypeExtension', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType',
'Compression', 'Filter', 'Interlace', 'BackgroundColor', 'ImageSize',
'Megapixels', 'ImageHeight'}
def get_meta(self):
out = subprocess.check_output(['exiftool', '-json', self.filename])
return json.loads(out)[0]
meta = json.loads(out)[0]
for key in self.meta_whitelist:
meta.pop(key, None)
return meta
def remove_all(self):
surface = cairo.ImageSurface.create_from_png(self.filename)
surface.write_to_png(self.output_filename)
return True

BIN
tests/data/dirty.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 580 KiB

View File

@ -5,7 +5,7 @@ import shutil
import os
from src import parsers
from src.parsers import pdf
from src.parsers import pdf, png
class TestGetMeta(unittest.TestCase):
def test_pdf(self):
@ -14,14 +14,15 @@ class TestGetMeta(unittest.TestCase):
self.assertEqual(meta['producer'], 'pdfTeX-1.40.14')
self.assertEqual(meta['creator'], "'Certified by IEEE PDFeXpress at 03/19/2016 2:56:07 AM'")
def test_png(self):
p = png.PNGParser('./tests/data/dirty.png')
meta = p.get_meta()
self.assertEqual(meta['Comment'], 'This is a comment, be careful!')
self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25")
class TestCleaning(unittest.TestCase):
def setUp(self):
shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf')
def tearDown(self):
os.remove('./tests/data/clean.pdf')
def test_pdf(self):
shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf')
p = pdf.PDFParser('./tests/data/clean.pdf')
meta = p.get_meta()
@ -33,3 +34,20 @@ class TestCleaning(unittest.TestCase):
p = pdf.PDFParser('./tests/data/clean.pdf.cleaned')
expected_meta = {'creation-date': -1, 'format': 'PDF-1.5', 'mod-date': -1}
self.assertEqual(p.get_meta(), expected_meta)
os.remove('./tests/data/clean.pdf')
def test_png(self):
shutil.copy('./tests/data/dirty.png', './tests/data/clean.png')
p = png.PNGParser('./tests/data/clean.png')
meta = p.get_meta()
self.assertEqual(meta['Comment'], 'This is a comment, be careful!')
ret = p.remove_all()
self.assertTrue(ret)
p = png.PNGParser('./tests/data/clean.png.cleaned')
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.png')