1
0
mirror of synced 2024-11-24 02:04:23 +01:00

Refactor tests

This commit is contained in:
jvoisin 2019-10-12 13:32:04 -07:00
parent 6d19a20935
commit 3cef7fe7fc
5 changed files with 313 additions and 538 deletions

View File

@ -29,8 +29,7 @@ class ExiftoolParser(abstract.AbstractParser):
def _lightweight_cleanup(self) -> bool: def _lightweight_cleanup(self) -> bool:
if os.path.exists(self.output_filename): if os.path.exists(self.output_filename):
try: try: # exiftool can't force output to existing files
# exiftool can't force output to existing files
os.remove(self.output_filename) os.remove(self.output_filename)
except OSError as e: # pragma: no cover except OSError as e: # pragma: no cover
logging.error("The output file %s is already existing and \ logging.error("The output file %s is already existing and \

View File

@ -120,7 +120,6 @@ class TestGetMeta(unittest.TestCase):
self.assertEqual(meta['4'], '# And an other one') self.assertEqual(meta['4'], '# And an other one')
self.assertEqual(meta['6'], '# and a final one here') self.assertEqual(meta['6'], '# and a final one here')
def test_tiff(self): def test_tiff(self):
p = images.TiffParser('./tests/data/dirty.tiff') p = images.TiffParser('./tests/data/dirty.tiff')
meta = p.get_meta() meta = p.get_meta()
@ -222,6 +221,10 @@ class TestGetMeta(unittest.TestCase):
self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!') self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')
os.remove('./tests/data/dirty.tar') os.remove('./tests/data/dirty.tar')
def test_svg(self):
p = images.SVGParser('./tests/data/weird.svg')
self.assertEqual(p.get_meta()['Xmlns'], 'http://www.w3.org/1337/svg')
class TestRemovingThumbnails(unittest.TestCase): class TestRemovingThumbnails(unittest.TestCase):
def test_odt(self): def test_odt(self):
@ -281,367 +284,215 @@ class TestRevisionsCleaning(unittest.TestCase):
os.remove('./tests/data/revision_clean.docx') os.remove('./tests/data/revision_clean.docx')
os.remove('./tests/data/revision_clean.cleaned.docx') os.remove('./tests/data/revision_clean.cleaned.docx')
class TestCleaning(unittest.TestCase): class TestCleaning(unittest.TestCase):
def test_pdf(self): data = [{
shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf') 'name': 'pdf',
p = pdf.PDFParser('./tests/data/clean.pdf') 'parser': pdf.PDFParser,
'meta': {'producer': 'pdfTeX-1.40.14'},
meta = p.get_meta() 'expected_meta': {'creation-date': -1, 'format': 'PDF-1.5', 'mod-date': -1},
self.assertEqual(meta['producer'], 'pdfTeX-1.40.14') }, {
'name': 'png',
ret = p.remove_all() 'parser': images.PNGParser,
self.assertTrue(ret) 'meta': {'Comment': 'This is a comment, be careful!'},
'expected_meta': {},
p = pdf.PDFParser('./tests/data/clean.cleaned.pdf') }, {
expected_meta = {'creation-date': -1, 'format': 'PDF-1.5', 'mod-date': -1} 'name': 'jpg',
self.assertEqual(p.get_meta(), expected_meta) 'parser': images.JPGParser,
self.assertTrue(p.remove_all()) 'meta': {'Comment': 'Created with GIMP'},
'expected_meta': {},
os.remove('./tests/data/clean.pdf') }, {
os.remove('./tests/data/clean.cleaned.pdf') 'name': 'mp3',
os.remove('./tests/data/clean.cleaned.cleaned.pdf') 'parser': audio.MP3Parser,
'meta': {'TXXX:I am a': 'various comment'},
def test_png(self): 'expected_meta': {},
shutil.copy('./tests/data/dirty.png', './tests/data/clean.png') }, {
p = images.PNGParser('./tests/data/clean.png') 'name': 'ogg',
'parser': audio.OGGParser,
meta = p.get_meta() 'meta': {'title': 'I am so'},
self.assertEqual(meta['Comment'], 'This is a comment, be careful!') 'expected_meta': {},
}, {
ret = p.remove_all() 'name': 'flac',
self.assertTrue(ret) 'parser': audio.FLACParser,
'meta': {'title': 'I am so'},
p = images.PNGParser('./tests/data/clean.cleaned.png') 'expected_meta': {},
self.assertEqual(p.get_meta(), {}) }, {
self.assertTrue(p.remove_all()) 'name': 'docx',
'parser': office.MSOfficeParser,
os.remove('./tests/data/clean.png') 'meta': {'word/media/image1.png' :
os.remove('./tests/data/clean.cleaned.png') {'Comment': 'This is a comment, be careful!',
os.remove('./tests/data/clean.cleaned.cleaned.png') 'ModifyDate': '2018:03:20 21:59:25',
'PixelUnits': 'meters',
def test_jpg(self): 'PixelsPerUnitX': 2835,
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') 'PixelsPerUnitY': 2835,
p = images.JPGParser('./tests/data/clean.jpg') 'create_system': 'Weird',
'date_time': '2018-03-31 13:15:38'} ,
meta = p.get_meta() },
self.assertEqual(meta['Comment'], 'Created with GIMP') 'expected_meta': {},
}, {
ret = p.remove_all() 'name': 'odt',
self.assertTrue(ret) 'parser': office.LibreOfficeParser,
'meta': {
p = images.JPGParser('./tests/data/clean.cleaned.jpg') 'Pictures/1000000000000032000000311EC5314D.png': {
self.assertEqual(p.get_meta(), {}) 'create_system': 'Weird',
self.assertTrue(p.remove_all()) 'date_time': '2011-07-26 02:40:16',
'PixelsPerUnitX': 4847,
os.remove('./tests/data/clean.jpg') 'PixelsPerUnitY': 4760,
os.remove('./tests/data/clean.cleaned.jpg') 'PixelUnits': 'meters',
os.remove('./tests/data/clean.cleaned.cleaned.jpg') },
},
def test_mp3(self): 'expected_meta': {},
shutil.copy('./tests/data/dirty.mp3', './tests/data/clean.mp3') },{
p = audio.MP3Parser('./tests/data/clean.mp3') 'name': 'tiff',
'parser': images.TiffParser,
meta = p.get_meta() 'meta': {'Model': 'C7070WZ'},
self.assertEqual(meta['TXXX:I am a'], 'various comment') 'expected_meta':
{'Orientation': 'Horizontal (normal)',
ret = p.remove_all() 'ResolutionUnit': 'inches',
self.assertTrue(ret) 'XResolution': 72,
'YResolution': 72}
p = audio.MP3Parser('./tests/data/clean.cleaned.mp3') },{
self.assertEqual(p.get_meta(), {}) 'name': 'bmp',
self.assertTrue(p.remove_all()) 'parser': harmless.HarmlessParser,
'meta': {},
os.remove('./tests/data/clean.mp3') 'expected_meta': {},
os.remove('./tests/data/clean.cleaned.mp3') },{
os.remove('./tests/data/clean.cleaned.cleaned.mp3') 'name': 'torrent',
'parser': torrent.TorrentParser,
def test_ogg(self): 'meta': {'created by': b'mktorrent 1.0', 'creation date': 1522397702},
shutil.copy('./tests/data/dirty.ogg', './tests/data/clean.ogg') 'expected_meta': {},
p = audio.OGGParser('./tests/data/clean.ogg') }, {
'name': 'odf',
meta = p.get_meta() 'parser': office.LibreOfficeParser,
self.assertEqual(meta['title'], 'I am so') 'meta': {'meta.xml': {'create_system': 'Weird', 'date_time':
'2018-04-22 22:20:24', 'meta:initial-creator': 'Julien Voisin',
ret = p.remove_all() 'meta:creation-date': '2018-04-23T00:18:59.438231281',
self.assertTrue(ret) 'dc:date': '2018-04-23T00:20:23.978564933', 'dc:creator':
'Julien Voisin', 'meta:editing-duration': 'PT1M24S',
p = audio.OGGParser('./tests/data/clean.cleaned.ogg') 'meta:editing-cycles': '1', 'meta:generator':
self.assertEqual(p.get_meta(), {}) 'LibreOffice/5.4.6.2$Linux_X86_64 LibreOffice_project/40m0$Build-2'}},
self.assertTrue(p.remove_all()) 'expected_meta': {},
}, {
os.remove('./tests/data/clean.ogg') 'name': 'odg',
os.remove('./tests/data/clean.cleaned.ogg') 'parser': office.LibreOfficeParser,
os.remove('./tests/data/clean.cleaned.cleaned.ogg') 'meta': {'meta.xml': {'create_system': 'Weird', 'date_time':
'2018-04-22 22:26:58', 'meta:initial-creator': 'Julien Voisin',
def test_flac(self): 'meta:creation-date': '2018-04-23T00:25:59.953271949',
shutil.copy('./tests/data/dirty.flac', './tests/data/clean.flac') 'dc:date': '2018-04-23T00:26:59.385838550', 'dc:creator':
p = audio.FLACParser('./tests/data/clean.flac') 'Julien Voisin', 'meta:editing-duration': 'PT59S',
'meta:editing-cycles': '1', 'meta:generator':
meta = p.get_meta() 'LibreOffice/5.4.6.2$Linux_X86_64 LibreOffice_project/40m0$Build-2'}},
self.assertEqual(meta['title'], 'I am so') 'expected_meta': {},
}, {
ret = p.remove_all() 'name': 'txt',
self.assertTrue(ret) 'parser': harmless.HarmlessParser,
'meta': {},
p = audio.FLACParser('./tests/data/clean.cleaned.flac') 'expected_meta': {},
self.assertEqual(p.get_meta(), {}) },{
self.assertTrue(p.remove_all()) 'name': 'gif',
'parser': images.GIFParser,
os.remove('./tests/data/clean.flac') 'meta': {'Comment': 'this is a test comment'},
os.remove('./tests/data/clean.cleaned.flac') 'expected_meta': {},
os.remove('./tests/data/clean.cleaned.cleaned.flac') },{
'name': 'css',
def test_office(self): 'parser': web.CSSParser,
shutil.copy('./tests/data/dirty.docx', './tests/data/clean.docx') 'meta': {
p = office.MSOfficeParser('./tests/data/clean.docx') 'harmful data': 'underline is cool',
'version': '1.0',
meta = p.get_meta() 'author': 'jvoisin'
self.assertIsNotNone(meta) },
'expected_meta': {},
ret = p.remove_all() },{
self.assertTrue(ret) 'name': 'svg',
'parser': images.SVGParser,
p = office.MSOfficeParser('./tests/data/clean.cleaned.docx') 'meta': {
self.assertEqual(p.get_meta(), {}) 'WorkDescription': "This is a test svg image for mat2's testsuite",
self.assertTrue(p.remove_all()) },
'expected_meta': {},
os.remove('./tests/data/clean.docx') } ,{
os.remove('./tests/data/clean.cleaned.docx') 'name': 'ppm',
os.remove('./tests/data/clean.cleaned.cleaned.docx') 'parser': images.PPMParser,
'meta': {
def test_libreoffice(self): '1': '# A metadata',
shutil.copy('./tests/data/dirty.odt', './tests/data/clean.odt') },
p = office.LibreOfficeParser('./tests/data/clean.odt') 'expected_meta': {},
} ,{
meta = p.get_meta() 'name': 'avi',
self.assertIsNotNone(meta) 'ffmpeg': 1,
'parser': video.AVIParser,
ret = p.remove_all() 'meta': {
self.assertTrue(ret) 'Software': 'MEncoder SVN-r33148-4.0.1',
},
p = office.LibreOfficeParser('./tests/data/clean.cleaned.odt') 'expected_meta': {},
self.assertEqual(p.get_meta(), {}) } ,{
self.assertTrue(p.remove_all()) 'name': 'mp4',
'ffmpeg': 1,
os.remove('./tests/data/clean.odt') 'parser': video.MP4Parser,
os.remove('./tests/data/clean.cleaned.odt') 'meta': {
os.remove('./tests/data/clean.cleaned.cleaned.odt') 'Encoder': 'HandBrake 0.9.4 2009112300',
},
def test_tiff(self): 'expected_meta':
shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff') {'CompatibleBrands': ['isom', 'iso2', 'avc1', 'mp41'],
p = images.TiffParser('./tests/data/clean.tiff') 'CompressorID': 'avc1',
'GraphicsMode': 'srcCopy',
meta = p.get_meta() 'HandlerDescription': 'SoundHandler',
self.assertEqual(meta['Model'], 'C7070WZ') 'HandlerType': 'Metadata',
'HandlerVendorID': 'Apple',
ret = p.remove_all() 'MajorBrand': 'MP4 Base Media v1 [IS0 14496-12:2003]',
self.assertTrue(ret) 'MediaHeaderVersion': 0,
'MinorVersion': '0.2.0',
p = images.TiffParser('./tests/data/clean.cleaned.tiff') 'MovieDataOffset': 48,
self.assertEqual(p.get_meta(), {}) 'MovieHeaderVersion': 0,
self.assertTrue(p.remove_all()) 'NextTrackID': 3,
'PreferredRate': 1,
os.remove('./tests/data/clean.tiff') 'Rotation': 0,
os.remove('./tests/data/clean.cleaned.tiff') 'TimeScale': 1000,
os.remove('./tests/data/clean.cleaned.cleaned.tiff') 'TrackHeaderVersion': 0,
'TrackID': 1,
def test_bmp(self): 'TrackLayer': 0},
shutil.copy('./tests/data/dirty.bmp', './tests/data/clean.bmp') },{
p = harmless.HarmlessParser('./tests/data/clean.bmp') 'name': 'wmv',
'ffmpeg': 1,
meta = p.get_meta() 'parser': video.WMVParser,
self.assertEqual(meta, {}) # bmp has no meta :) 'meta': {
'EncodingSettings': 'Lavf52.103.0',
ret = p.remove_all() },
self.assertTrue(ret) 'expected_meta': {},
}
p = harmless.HarmlessParser('./tests/data/clean.cleaned.bmp') ]
self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all()) def test_all_parametred(self):
for case in self.data:
os.remove('./tests/data/clean.bmp') if 'ffmpeg' in case:
os.remove('./tests/data/clean.cleaned.bmp')
os.remove('./tests/data/clean.cleaned.cleaned.bmp')
def test_torrent(self):
shutil.copy('./tests/data/dirty.torrent', './tests/data/clean.torrent')
p = torrent.TorrentParser('./tests/data/clean.torrent')
meta = p.get_meta()
self.assertEqual(meta, {'created by': b'mktorrent 1.0', 'creation date': 1522397702})
ret = p.remove_all()
self.assertTrue(ret)
p = torrent.TorrentParser('./tests/data/clean.cleaned.torrent')
self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.torrent')
os.remove('./tests/data/clean.cleaned.torrent')
os.remove('./tests/data/clean.cleaned.cleaned.torrent')
def test_odf(self):
shutil.copy('./tests/data/dirty.odf', './tests/data/clean.odf')
p = office.LibreOfficeParser('./tests/data/clean.odf')
meta = p.get_meta()
self.assertEqual(meta['meta.xml']['meta:creation-date'], '2018-04-23T00:18:59.438231281')
ret = p.remove_all()
self.assertTrue(ret)
p = office.LibreOfficeParser('./tests/data/clean.cleaned.odf')
self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.odf')
os.remove('./tests/data/clean.cleaned.odf')
os.remove('./tests/data/clean.cleaned.cleaned.odf')
def test_odg(self):
shutil.copy('./tests/data/dirty.odg', './tests/data/clean.odg')
p = office.LibreOfficeParser('./tests/data/clean.odg')
meta = p.get_meta()
self.assertEqual(meta['meta.xml']['dc:date'], '2018-04-23T00:26:59.385838550')
ret = p.remove_all()
self.assertTrue(ret)
p = office.LibreOfficeParser('./tests/data/clean.cleaned.odg')
self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.odg')
os.remove('./tests/data/clean.cleaned.odg')
os.remove('./tests/data/clean.cleaned.cleaned.odg')
def test_txt(self):
shutil.copy('./tests/data/dirty.txt', './tests/data/clean.txt')
p = harmless.HarmlessParser('./tests/data/clean.txt')
meta = p.get_meta()
self.assertEqual(meta, {})
ret = p.remove_all()
self.assertTrue(ret)
p = harmless.HarmlessParser('./tests/data/clean.cleaned.txt')
self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.txt')
os.remove('./tests/data/clean.cleaned.txt')
os.remove('./tests/data/clean.cleaned.cleaned.txt')
def test_avi(self):
try: try:
video._get_ffmpeg_path() video._get_ffmpeg_path()
except RuntimeError: except RuntimeError:
raise unittest.SkipTest raise unittest.SkipTest
shutil.copy('./tests/data/dirty.avi', './tests/data/clean.avi') print('[+] Testing %s' % case['name'])
p = video.AVIParser('./tests/data/clean.avi') target = './tests/data/clean.' + case['name']
shutil.copy('./tests/data/dirty.' + case['name'], target)
p1 = case['parser'](target)
meta = p.get_meta() meta = p1.get_meta()
self.assertEqual(meta['Software'], 'MEncoder SVN-r33148-4.0.1') for k, v in case['meta'].items():
if isinstance(v, dict):
for _k, _v in v.items():
self.assertEqual(meta[k][_k], _v)
else:
self.assertEqual(meta[k], v)
ret = p.remove_all() p1.lightweight_cleaning = True
self.assertTrue(ret) self.assertTrue(p1.remove_all())
p = video.AVIParser('./tests/data/clean.cleaned.avi') p2 = case['parser'](p1.output_filename)
self.assertEqual(p.get_meta(), {}) self.assertEqual(p2.get_meta(), case['expected_meta'])
self.assertTrue(p.remove_all()) self.assertTrue(p2.remove_all())
os.remove('./tests/data/clean.avi') os.remove(target)
os.remove('./tests/data/clean.cleaned.avi') os.remove(p1.output_filename)
os.remove('./tests/data/clean.cleaned.cleaned.avi') os.remove(p2.output_filename)
def test_zip(self):
with zipfile.ZipFile('./tests/data/dirty.zip', 'w') as zout:
zout.write('./tests/data/dirty.flac')
zout.write('./tests/data/dirty.docx')
zout.write('./tests/data/dirty.jpg')
p = archive.ZipParser('./tests/data/dirty.zip')
meta = p.get_meta()
self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')
ret = p.remove_all()
self.assertTrue(ret)
p = archive.ZipParser('./tests/data/dirty.cleaned.zip')
self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all())
os.remove('./tests/data/dirty.zip')
os.remove('./tests/data/dirty.cleaned.zip')
os.remove('./tests/data/dirty.cleaned.cleaned.zip')
def test_mp4(self):
try:
video._get_ffmpeg_path()
except RuntimeError:
raise unittest.SkipTest
shutil.copy('./tests/data/dirty.mp4', './tests/data/clean.mp4')
p = video.MP4Parser('./tests/data/clean.mp4')
meta = p.get_meta()
self.assertEqual(meta['Encoder'], 'HandBrake 0.9.4 2009112300')
ret = p.remove_all()
self.assertTrue(ret)
p = video.MP4Parser('./tests/data/clean.cleaned.mp4')
self.assertNotIn('Encoder', p.get_meta())
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.mp4')
os.remove('./tests/data/clean.cleaned.mp4')
os.remove('./tests/data/clean.cleaned.cleaned.mp4')
def test_wmv(self):
try:
video._get_ffmpeg_path()
except RuntimeError:
raise unittest.SkipTest
shutil.copy('./tests/data/dirty.wmv', './tests/data/clean.wmv')
p = video.WMVParser('./tests/data/clean.wmv')
meta = p.get_meta()
self.assertEqual(meta['EncodingSettings'], 'Lavf52.103.0')
ret = p.remove_all()
self.assertTrue(ret)
p = video.WMVParser('./tests/data/clean.cleaned.wmv')
self.assertNotIn('EncodingSettings', p.get_meta())
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.wmv')
os.remove('./tests/data/clean.cleaned.wmv')
os.remove('./tests/data/clean.cleaned.cleaned.wmv')
def test_gif(self):
shutil.copy('./tests/data/dirty.gif', './tests/data/clean.gif')
p = images.GIFParser('./tests/data/clean.gif')
meta = p.get_meta()
self.assertEqual(meta['Comment'], 'this is a test comment')
ret = p.remove_all()
self.assertTrue(ret)
p = images.GIFParser('./tests/data/clean.cleaned.gif')
self.assertNotIn('EncodingSettings', p.get_meta())
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.gif')
os.remove('./tests/data/clean.cleaned.gif')
os.remove('./tests/data/clean.cleaned.cleaned.gif')
def test_html(self): def test_html(self):
shutil.copy('./tests/data/dirty.html', './tests/data/clean.html') shutil.copy('./tests/data/dirty.html', './tests/data/clean.html')
@ -688,7 +539,6 @@ class TestCleaning(unittest.TestCase):
os.remove('./tests/data/clean.html') os.remove('./tests/data/clean.html')
os.remove('./tests/data/clean.cleaned.html') os.remove('./tests/data/clean.cleaned.html')
def test_epub(self): def test_epub(self):
shutil.copy('./tests/data/dirty.epub', './tests/data/clean.epub') shutil.copy('./tests/data/dirty.epub', './tests/data/clean.epub')
p = epub.EPUBParser('./tests/data/clean.epub') p = epub.EPUBParser('./tests/data/clean.epub')
@ -711,25 +561,26 @@ class TestCleaning(unittest.TestCase):
os.remove('./tests/data/clean.cleaned.cleaned.epub') os.remove('./tests/data/clean.cleaned.cleaned.epub')
def test_css(self): class TestCleaningArchives(unittest.TestCase):
shutil.copy('./tests/data/dirty.css', './tests/data/clean.css') def test_zip(self):
p = web.CSSParser('./tests/data/clean.css') with zipfile.ZipFile('./tests/data/dirty.zip', 'w') as zout:
zout.write('./tests/data/dirty.flac')
self.assertEqual(p.get_meta(), { zout.write('./tests/data/dirty.docx')
'harmful data': 'underline is cool', zout.write('./tests/data/dirty.jpg')
'version': '1.0', p = archive.ZipParser('./tests/data/dirty.zip')
'author': 'jvoisin'}) meta = p.get_meta()
self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')
ret = p.remove_all() ret = p.remove_all()
self.assertTrue(ret) self.assertTrue(ret)
p = web.CSSParser('./tests/data/clean.cleaned.css') p = archive.ZipParser('./tests/data/dirty.cleaned.zip')
self.assertEqual(p.get_meta(), {}) self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all()) self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.css') os.remove('./tests/data/dirty.zip')
os.remove('./tests/data/clean.cleaned.css') os.remove('./tests/data/dirty.cleaned.zip')
os.remove('./tests/data/clean.cleaned.cleaned.css') os.remove('./tests/data/dirty.cleaned.cleaned.zip')
def test_tar(self): def test_tar(self):
with tarfile.TarFile.open('./tests/data/dirty.tar', 'w') as zout: with tarfile.TarFile.open('./tests/data/dirty.tar', 'w') as zout:
@ -870,49 +721,3 @@ class TestCleaning(unittest.TestCase):
os.remove('./tests/data/dirty.tar.xz') os.remove('./tests/data/dirty.tar.xz')
os.remove('./tests/data/dirty.cleaned.tar.xz') os.remove('./tests/data/dirty.cleaned.tar.xz')
os.remove('./tests/data/dirty.cleaned.cleaned.tar.xz') os.remove('./tests/data/dirty.cleaned.cleaned.tar.xz')
def test_svg(self):
shutil.copy('./tests/data/dirty.svg', './tests/data/clean.svg')
p = images.SVGParser('./tests/data/clean.svg')
meta = p.get_meta()
self.assertEqual(meta['WorkCreatorAgentTitle'], 'GNOME Design Team')
self.assertEqual(meta['WorkSubject'], ['mat2', 'logo', 'metadata'])
self.assertEqual(meta['ID'], 'svg11300')
self.assertEqual(meta['Output_extension'],
'org.inkscape.output.svg.inkscape')
ret = p.remove_all()
self.assertTrue(ret)
p = images.SVGParser('./tests/data/clean.cleaned.svg')
self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.svg')
os.remove('./tests/data/clean.cleaned.svg')
os.remove('./tests/data/clean.cleaned.cleaned.svg')
p = images.SVGParser('./tests/data/weird.svg')
self.assertEqual(p.get_meta()['Xmlns'], 'http://www.w3.org/1337/svg')
def test_ppm(self):
shutil.copy('./tests/data/dirty.ppm', './tests/data/clean.ppm')
p = images.PPMParser('./tests/data/clean.ppm')
meta = p.get_meta()
print(meta)
self.assertEqual(meta['1'], '# A metadata')
ret = p.remove_all()
self.assertTrue(ret)
p = images.PPMParser('./tests/data/clean.cleaned.ppm')
self.assertEqual(p.get_meta(), {})
self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.ppm')
os.remove('./tests/data/clean.cleaned.ppm')
os.remove('./tests/data/clean.cleaned.cleaned.ppm')

View File

@ -1,106 +0,0 @@
#!/usr/bin/env python3
import unittest
import shutil
import os
from libmat2 import pdf, images, torrent
class TestLightWeightCleaning(unittest.TestCase):
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()
self.assertEqual(meta['producer'], 'pdfTeX-1.40.14')
p.lightweight_cleaning = True
ret = p.remove_all()
self.assertTrue(ret)
p = pdf.PDFParser('./tests/data/clean.cleaned.pdf')
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')
os.remove('./tests/data/clean.cleaned.pdf')
def test_png(self):
shutil.copy('./tests/data/dirty.png', './tests/data/clean.png')
p = images.PNGParser('./tests/data/clean.png')
meta = p.get_meta()
self.assertEqual(meta['Comment'], 'This is a comment, be careful!')
p.lightweight_cleaning = True
ret = p.remove_all()
self.assertTrue(ret)
p = images.PNGParser('./tests/data/clean.cleaned.png')
self.assertEqual(p.get_meta(), {})
p = images.PNGParser('./tests/data/clean.png')
p.lightweight_cleaning = True
ret = p.remove_all()
self.assertTrue(ret)
os.remove('./tests/data/clean.png')
os.remove('./tests/data/clean.cleaned.png')
def test_jpg(self):
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
p = images.JPGParser('./tests/data/clean.jpg')
meta = p.get_meta()
self.assertEqual(meta['Comment'], 'Created with GIMP')
p.lightweight_cleaning = True
ret = p.remove_all()
self.assertTrue(ret)
p = images.JPGParser('./tests/data/clean.cleaned.jpg')
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.jpg')
os.remove('./tests/data/clean.cleaned.jpg')
def test_torrent(self):
shutil.copy('./tests/data/dirty.torrent', './tests/data/clean.torrent')
p = torrent.TorrentParser('./tests/data/clean.torrent')
meta = p.get_meta()
self.assertEqual(meta['created by'], b'mktorrent 1.0')
p.lightweight_cleaning = True
ret = p.remove_all()
self.assertTrue(ret)
p = torrent.TorrentParser('./tests/data/clean.cleaned.torrent')
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.torrent')
os.remove('./tests/data/clean.cleaned.torrent')
def test_tiff(self):
shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff')
p = images.TiffParser('./tests/data/clean.tiff')
meta = p.get_meta()
self.assertEqual(meta['ImageDescription'], 'OLYMPUS DIGITAL CAMERA ')
p.lightweight_cleaning = True
ret = p.remove_all()
self.assertTrue(ret)
p = images.TiffParser('./tests/data/clean.cleaned.tiff')
self.assertEqual(p.get_meta(),
{
'Orientation': 'Horizontal (normal)',
'ResolutionUnit': 'inches',
'XResolution': 72,
'YResolution': 72
}
)
os.remove('./tests/data/clean.tiff')
os.remove('./tests/data/clean.cleaned.tiff')

View File

@ -0,0 +1,76 @@
#!/usr/bin/env python3
import unittest
import shutil
import os
from libmat2 import pdf, images, torrent
class TestLightWeightCleaning(unittest.TestCase):
data = [{
'name': 'pdf',
'parser': pdf.PDFParser,
'meta': {'producer': 'pdfTeX-1.40.14'},
'expected_meta': {'creation-date': -1, 'format': 'PDF-1.5', 'mod-date': -1},
}, {
'name': 'png',
'parser': images.PNGParser,
'meta': {'Comment': 'This is a comment, be careful!'},
'expected_meta': {},
}, {
'name': 'jpg',
'parser': images.JPGParser,
'meta': {'Comment': 'Created with GIMP'},
'expected_meta': {},
}, {
'name': 'torrent',
'parser': torrent.TorrentParser,
'meta': {'created by': b'mktorrent 1.0'},
'expected_meta': {},
},{
'name': 'tiff',
'parser': images.TiffParser,
'meta': {'ImageDescription': 'OLYMPUS DIGITAL CAMERA '},
'expected_meta': {
'Orientation': 'Horizontal (normal)',
'ResolutionUnit': 'inches',
'XResolution': 72,
'YResolution': 72
}
},
]
def test_all(self):
for case in self.data:
target = './tests/data/clean.' + case['name']
shutil.copy('./tests/data/dirty.' + case['name'], target)
p1 = case['parser'](target)
meta = p1.get_meta()
for k, v in case['meta'].items():
self.assertEqual(meta[k], v)
p1.lightweight_cleaning = True
self.assertTrue(p1.remove_all())
p2 = case['parser'](p1.output_filename)
self.assertEqual(p2.get_meta(), case['expected_meta'])
os.remove(target)
os.remove(p1.output_filename)
def test_exiftool_overwrite(self):
target = './tests/data/clean.png'
shutil.copy('./tests/data/dirty.png', target)
p1 = images.PNGParser(target)
p1.lightweight_cleaning = True
shutil.copy('./tests/data/dirty.png', p1.output_filename)
self.assertTrue(p1.remove_all())
p2 = images.PNGParser(p1.output_filename)
self.assertEqual(p2.get_meta(), {})
os.remove(target)
os.remove(p1.output_filename)

View File

@ -7,25 +7,26 @@ import os
from libmat2 import office, UnknownMemberPolicy from libmat2 import office, UnknownMemberPolicy
class TestPolicy(unittest.TestCase): class TestPolicy(unittest.TestCase):
target = './tests/data/clean.docx'
def test_policy_omit(self): def test_policy_omit(self):
shutil.copy('./tests/data/embedded.docx', './tests/data/clean.docx') shutil.copy('./tests/data/embedded.docx', self.target)
p = office.MSOfficeParser('./tests/data/clean.docx') p = office.MSOfficeParser(self.target)
p.unknown_member_policy = UnknownMemberPolicy.OMIT p.unknown_member_policy = UnknownMemberPolicy.OMIT
self.assertTrue(p.remove_all()) self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.docx') os.remove(p.filename)
os.remove('./tests/data/clean.cleaned.docx')
def test_policy_keep(self): def test_policy_keep(self):
shutil.copy('./tests/data/embedded.docx', './tests/data/clean.docx') shutil.copy('./tests/data/embedded.docx', self.target)
p = office.MSOfficeParser('./tests/data/clean.docx') p = office.MSOfficeParser(self.target)
p.unknown_member_policy = UnknownMemberPolicy.KEEP p.unknown_member_policy = UnknownMemberPolicy.KEEP
self.assertTrue(p.remove_all()) self.assertTrue(p.remove_all())
os.remove('./tests/data/clean.docx') os.remove(p.filename)
os.remove('./tests/data/clean.cleaned.docx') os.remove(p.output_filename)
def test_policy_unknown(self): def test_policy_unknown(self):
shutil.copy('./tests/data/embedded.docx', './tests/data/clean.docx') shutil.copy('./tests/data/embedded.docx', self.target)
p = office.MSOfficeParser('./tests/data/clean.docx') p = office.MSOfficeParser(self.target)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
p.unknown_member_policy = UnknownMemberPolicy('unknown_policy_name_totally_invalid') p.unknown_member_policy = UnknownMemberPolicy('unknown_policy_name_totally_invalid')
os.remove('./tests/data/clean.docx') os.remove(p.filename)