diff options
| -rw-r--r-- | libmat2/archive.py | 5 | ||||
| -rw-r--r-- | tests/test_corrupted_files.py | 15 | ||||
| -rw-r--r-- | tests/test_libmat2.py | 35 |
3 files changed, 54 insertions, 1 deletions
diff --git a/libmat2/archive.py b/libmat2/archive.py index b4700c3..bcf8d33 100644 --- a/libmat2/archive.py +++ b/libmat2/archive.py | |||
| @@ -157,3 +157,8 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser): | |||
| 157 | os.remove(self.output_filename) | 157 | os.remove(self.output_filename) |
| 158 | return False | 158 | return False |
| 159 | return True | 159 | return True |
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | class ZipParser(ArchiveBasedAbstractParser): | ||
| 164 | mimetypes = {'application/zip'} | ||
diff --git a/tests/test_corrupted_files.py b/tests/test_corrupted_files.py index 181d4d2..e7d3c2a 100644 --- a/tests/test_corrupted_files.py +++ b/tests/test_corrupted_files.py | |||
| @@ -4,6 +4,7 @@ import unittest | |||
| 4 | import shutil | 4 | import shutil |
| 5 | import os | 5 | import os |
| 6 | import logging | 6 | import logging |
| 7 | import zipfile | ||
| 7 | 8 | ||
| 8 | from libmat2 import pdf, images, audio, office, parser_factory, torrent | 9 | from libmat2 import pdf, images, audio, office, parser_factory, torrent |
| 9 | from libmat2 import harmless, video | 10 | from libmat2 import harmless, video |
| @@ -222,3 +223,17 @@ class TestCorruptedFiles(unittest.TestCase): | |||
| 222 | p = video.AVIParser('./tests/data/--output.avi') | 223 | p = video.AVIParser('./tests/data/--output.avi') |
| 223 | self.assertFalse(p.remove_all()) | 224 | self.assertFalse(p.remove_all()) |
| 224 | os.remove('./tests/data/--output.avi') | 225 | os.remove('./tests/data/--output.avi') |
| 226 | |||
| 227 | def test_zip(self): | ||
| 228 | with zipfile.ZipFile('./tests/data/dirty.zip', 'w') as zout: | ||
| 229 | zout.write('./tests/data/dirty.flac') | ||
| 230 | zout.write('./tests/data/dirty.docx') | ||
| 231 | zout.write('./tests/data/dirty.jpg') | ||
| 232 | zout.write('./tests/data/embedded_corrupted.docx') | ||
| 233 | p, mimetype = parser_factory.get_parser('./tests/data/dirty.zip') | ||
| 234 | self.assertEqual(mimetype, 'application/zip') | ||
| 235 | meta = p.get_meta() | ||
| 236 | self.assertEqual(meta['tests/data/dirty.flac']['comments'], 'Thank you for using MAT !') | ||
| 237 | self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!') | ||
| 238 | self.assertFalse(p.remove_all()) | ||
| 239 | os.remove('./tests/data/dirty.zip') | ||
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index 46d6aaa..1602480 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -6,7 +6,7 @@ import os | |||
| 6 | import zipfile | 6 | import zipfile |
| 7 | 7 | ||
| 8 | from libmat2 import pdf, images, audio, office, parser_factory, torrent, harmless | 8 | from libmat2 import pdf, images, audio, office, parser_factory, torrent, harmless |
| 9 | from libmat2 import check_dependencies, video | 9 | from libmat2 import check_dependencies, video, archive |
| 10 | 10 | ||
| 11 | 11 | ||
| 12 | class TestCheckDependencies(unittest.TestCase): | 12 | class TestCheckDependencies(unittest.TestCase): |
| @@ -153,6 +153,18 @@ class TestGetMeta(unittest.TestCase): | |||
| 153 | meta = p.get_meta() | 153 | meta = p.get_meta() |
| 154 | self.assertEqual(meta, {}) | 154 | self.assertEqual(meta, {}) |
| 155 | 155 | ||
| 156 | def test_zip(self): | ||
| 157 | with zipfile.ZipFile('./tests/data/dirty.zip', 'w') as zout: | ||
| 158 | zout.write('./tests/data/dirty.flac') | ||
| 159 | zout.write('./tests/data/dirty.docx') | ||
| 160 | zout.write('./tests/data/dirty.jpg') | ||
| 161 | p, mimetype = parser_factory.get_parser('./tests/data/dirty.zip') | ||
| 162 | self.assertEqual(mimetype, 'application/zip') | ||
| 163 | meta = p.get_meta() | ||
| 164 | self.assertEqual(meta['tests/data/dirty.flac']['comments'], 'Thank you for using MAT !') | ||
| 165 | self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!') | ||
| 166 | os.remove('./tests/data/dirty.zip') | ||
| 167 | |||
| 156 | 168 | ||
| 157 | class TestRemovingThumbnails(unittest.TestCase): | 169 | class TestRemovingThumbnails(unittest.TestCase): |
| 158 | def test_odt(self): | 170 | def test_odt(self): |
| @@ -488,3 +500,24 @@ class TestCleaning(unittest.TestCase): | |||
| 488 | os.remove('./tests/data/clean.avi') | 500 | os.remove('./tests/data/clean.avi') |
| 489 | os.remove('./tests/data/clean.cleaned.avi') | 501 | os.remove('./tests/data/clean.cleaned.avi') |
| 490 | os.remove('./tests/data/clean.cleaned.cleaned.avi') | 502 | os.remove('./tests/data/clean.cleaned.cleaned.avi') |
| 503 | |||
| 504 | def test_zip(self): | ||
| 505 | with zipfile.ZipFile('./tests/data/dirty.zip', 'w') as zout: | ||
| 506 | zout.write('./tests/data/dirty.flac') | ||
| 507 | zout.write('./tests/data/dirty.docx') | ||
| 508 | zout.write('./tests/data/dirty.jpg') | ||
| 509 | p = archive.ZipParser('./tests/data/dirty.zip') | ||
| 510 | meta = p.get_meta() | ||
| 511 | self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!') | ||
| 512 | |||
| 513 | ret = p.remove_all() | ||
| 514 | self.assertTrue(ret) | ||
| 515 | |||
| 516 | p = archive.ZipParser('./tests/data/dirty.cleaned.zip') | ||
| 517 | self.assertEqual(p.get_meta(), {}) | ||
| 518 | self.assertTrue(p.remove_all()) | ||
| 519 | |||
| 520 | os.remove('./tests/data/dirty.zip') | ||
| 521 | os.remove('./tests/data/dirty.cleaned.zip') | ||
| 522 | os.remove('./tests/data/dirty.cleaned.cleaned.zip') | ||
| 523 | |||
