2018-09-11 15:54:53 +02:00
|
|
|
#!/usr/bin/env python3
|
2018-06-22 21:16:55 +02:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
import shutil
|
|
|
|
import os
|
|
|
|
|
2018-07-06 00:49:17 +02:00
|
|
|
from libmat2 import pdf, images, audio, office, parser_factory, torrent, harmless
|
2018-06-22 21:16:55 +02:00
|
|
|
|
|
|
|
|
2018-07-30 22:36:36 +02:00
|
|
|
class TestInexistentFiles(unittest.TestCase):
|
|
|
|
def test_ro(self):
|
|
|
|
parser, mimetype = parser_factory.get_parser('/etc/passwd')
|
|
|
|
self.assertEqual(mimetype, None)
|
|
|
|
self.assertEqual(parser, None)
|
|
|
|
|
|
|
|
def test_notaccessible(self):
|
|
|
|
parser, mimetype = parser_factory.get_parser('/etc/shadow')
|
|
|
|
self.assertEqual(mimetype, None)
|
|
|
|
self.assertEqual(parser, None)
|
|
|
|
|
|
|
|
def test_folder(self):
|
|
|
|
parser, mimetype = parser_factory.get_parser('./tests/')
|
|
|
|
self.assertEqual(mimetype, None)
|
|
|
|
self.assertEqual(parser, None)
|
|
|
|
|
|
|
|
def test_inexistingfile(self):
|
|
|
|
parser, mimetype = parser_factory.get_parser('./tests/NONEXISTING_FILE')
|
|
|
|
self.assertEqual(mimetype, None)
|
|
|
|
self.assertEqual(parser, None)
|
|
|
|
|
|
|
|
def test_chardevice(self):
|
|
|
|
parser, mimetype = parser_factory.get_parser('/dev/zero')
|
|
|
|
self.assertEqual(mimetype, None)
|
|
|
|
self.assertEqual(parser, None)
|
|
|
|
|
|
|
|
def test_brokensymlink(self):
|
|
|
|
shutil.copy('./tests/test_libmat2.py', './tests/clean.py')
|
|
|
|
os.symlink('./tests/clean.py', './tests/SYMLINK')
|
|
|
|
os.remove('./tests/clean.py')
|
|
|
|
parser, mimetype = parser_factory.get_parser('./tests/SYMLINK')
|
|
|
|
self.assertEqual(mimetype, None)
|
|
|
|
self.assertEqual(parser, None)
|
|
|
|
os.unlink('./tests/SYMLINK')
|
|
|
|
|
2018-06-22 21:16:55 +02:00
|
|
|
class TestUnsupportedFiles(unittest.TestCase):
|
|
|
|
def test_pdf(self):
|
|
|
|
shutil.copy('./tests/test_libmat2.py', './tests/clean.py')
|
|
|
|
parser, mimetype = parser_factory.get_parser('./tests/data/clean.py')
|
|
|
|
self.assertEqual(mimetype, 'text/x-python')
|
|
|
|
self.assertEqual(parser, None)
|
|
|
|
os.remove('./tests/clean.py')
|
|
|
|
|
2018-07-08 21:35:45 +02:00
|
|
|
class TestCorruptedEmbedded(unittest.TestCase):
|
|
|
|
def test_docx(self):
|
|
|
|
shutil.copy('./tests/data/embedded_corrupted.docx', './tests/data/clean.docx')
|
2018-09-24 19:50:24 +02:00
|
|
|
parser, _ = parser_factory.get_parser('./tests/data/clean.docx')
|
2018-07-08 21:35:45 +02:00
|
|
|
self.assertFalse(parser.remove_all())
|
|
|
|
self.assertIsNotNone(parser.get_meta())
|
|
|
|
os.remove('./tests/data/clean.docx')
|
|
|
|
|
|
|
|
def test_odt(self):
|
2018-09-24 19:50:24 +02:00
|
|
|
expected = {
|
|
|
|
'create_system': 'Weird',
|
|
|
|
'date_time': '2018-06-10 17:18:18',
|
|
|
|
'meta.xml': 'harmful content'
|
|
|
|
}
|
2018-07-08 21:35:45 +02:00
|
|
|
shutil.copy('./tests/data/embedded_corrupted.odt', './tests/data/clean.odt')
|
2018-09-24 19:50:24 +02:00
|
|
|
parser, _ = parser_factory.get_parser('./tests/data/clean.odt')
|
2018-07-08 21:35:45 +02:00
|
|
|
self.assertFalse(parser.remove_all())
|
2018-09-24 19:50:24 +02:00
|
|
|
self.assertEqual(parser.get_meta(), expected)
|
2018-07-08 21:35:45 +02:00
|
|
|
os.remove('./tests/data/clean.odt')
|
|
|
|
|
2018-06-22 21:16:55 +02:00
|
|
|
|
|
|
|
class TestExplicitelyUnsupportedFiles(unittest.TestCase):
|
|
|
|
def test_pdf(self):
|
2018-07-06 00:42:09 +02:00
|
|
|
shutil.copy('./tests/test_libmat2.py', './tests/data/clean.py')
|
|
|
|
parser, mimetype = parser_factory.get_parser('./tests/data/clean.py')
|
|
|
|
self.assertEqual(mimetype, 'text/x-python')
|
2018-06-22 21:16:55 +02:00
|
|
|
self.assertEqual(parser, None)
|
2018-07-06 00:42:09 +02:00
|
|
|
os.remove('./tests/data/clean.py')
|
2018-06-22 21:16:55 +02:00
|
|
|
|
|
|
|
|
2018-09-30 19:52:35 +02:00
|
|
|
class TestCorruptedContentTypesOffice(unittest.TestCase):
|
|
|
|
def test_office(self):
|
|
|
|
shutil.copy('./tests/data/malformed_content_types.docx', './tests/data/clean.docx')
|
|
|
|
p = office.MSOfficeParser('./tests/data/clean.docx')
|
|
|
|
self.assertIsNotNone(p)
|
|
|
|
self.assertFalse(p.remove_all())
|
|
|
|
os.remove('./tests/data/clean.docx')
|
|
|
|
|
2018-06-22 21:16:55 +02:00
|
|
|
class TestCorruptedFiles(unittest.TestCase):
|
|
|
|
def test_pdf(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.png')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
pdf.PDFParser('./tests/data/clean.png')
|
|
|
|
os.remove('./tests/data/clean.png')
|
|
|
|
|
|
|
|
def test_png(self):
|
|
|
|
shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
images.PNGParser('./tests/data/clean.pdf')
|
|
|
|
os.remove('./tests/data/clean.pdf')
|
|
|
|
|
|
|
|
def test_png2(self):
|
|
|
|
shutil.copy('./tests/test_libmat2.py', './tests/clean.png')
|
2018-09-24 19:50:24 +02:00
|
|
|
parser, _ = parser_factory.get_parser('./tests/clean.png')
|
2018-06-22 21:16:55 +02:00
|
|
|
self.assertIsNone(parser)
|
|
|
|
os.remove('./tests/clean.png')
|
|
|
|
|
|
|
|
def test_torrent(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.torrent')
|
2018-07-08 13:47:00 +02:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
torrent.TorrentParser('./tests/data/clean.torrent')
|
2018-06-22 21:16:55 +02:00
|
|
|
|
|
|
|
with open("./tests/data/clean.torrent", "a") as f:
|
|
|
|
f.write("trailing garbage")
|
2018-07-08 13:47:00 +02:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
torrent.TorrentParser('./tests/data/clean.torrent')
|
|
|
|
|
2018-07-08 15:13:03 +02:00
|
|
|
with open("./tests/data/clean.torrent", "w") as f:
|
|
|
|
f.write("i-0e")
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
torrent.TorrentParser('./tests/data/clean.torrent')
|
|
|
|
|
|
|
|
with open("./tests/data/clean.torrent", "w") as f:
|
|
|
|
f.write("i00e")
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
torrent.TorrentParser('./tests/data/clean.torrent')
|
|
|
|
|
|
|
|
with open("./tests/data/clean.torrent", "w") as f:
|
2018-07-08 22:27:37 +02:00
|
|
|
f.write("01:AAAAAAAAA")
|
2018-07-08 15:13:03 +02:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
torrent.TorrentParser('./tests/data/clean.torrent')
|
|
|
|
|
|
|
|
with open("./tests/data/clean.torrent", "w") as f:
|
|
|
|
f.write("1:aaa")
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
torrent.TorrentParser('./tests/data/clean.torrent')
|
|
|
|
|
2018-06-22 21:16:55 +02:00
|
|
|
os.remove('./tests/data/clean.torrent')
|
|
|
|
|
|
|
|
def test_odg(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.odg')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
office.LibreOfficeParser('./tests/data/clean.odg')
|
|
|
|
os.remove('./tests/data/clean.odg')
|
|
|
|
|
|
|
|
def test_bmp(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.bmp')
|
2018-09-24 19:50:24 +02:00
|
|
|
ret = harmless.HarmlessParser('./tests/data/clean.bmp')
|
|
|
|
self.assertIsNotNone(ret)
|
2018-06-22 21:16:55 +02:00
|
|
|
os.remove('./tests/data/clean.bmp')
|
|
|
|
|
|
|
|
def test_docx(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.docx')
|
|
|
|
with self.assertRaises(ValueError):
|
2018-09-24 20:15:07 +02:00
|
|
|
office.MSOfficeParser('./tests/data/clean.docx')
|
2018-06-22 21:16:55 +02:00
|
|
|
os.remove('./tests/data/clean.docx')
|
|
|
|
|
|
|
|
def test_flac(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.flac')
|
|
|
|
with self.assertRaises(ValueError):
|
2018-09-24 20:15:07 +02:00
|
|
|
audio.FLACParser('./tests/data/clean.flac')
|
2018-06-22 21:16:55 +02:00
|
|
|
os.remove('./tests/data/clean.flac')
|
|
|
|
|
|
|
|
def test_mp3(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.mp3')
|
|
|
|
with self.assertRaises(ValueError):
|
2018-09-24 20:15:07 +02:00
|
|
|
audio.MP3Parser('./tests/data/clean.mp3')
|
2018-06-22 21:16:55 +02:00
|
|
|
os.remove('./tests/data/clean.mp3')
|
2018-07-07 18:02:53 +02:00
|
|
|
|
|
|
|
def test_jpg(self):
|
|
|
|
shutil.copy('./tests/data/dirty.mp3', './tests/data/clean.jpg')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
images.JPGParser('./tests/data/clean.jpg')
|
|
|
|
os.remove('./tests/data/clean.jpg')
|