2018-09-11 15:54:53 +02:00
|
|
|
#!/usr/bin/env python3
|
2018-06-22 21:16:55 +02:00
|
|
|
|
|
|
|
import unittest
|
2019-04-28 19:25:45 +02:00
|
|
|
import stat
|
2019-04-27 13:05:36 +02:00
|
|
|
import time
|
2018-06-22 21:16:55 +02:00
|
|
|
import shutil
|
|
|
|
import os
|
2018-10-02 15:26:13 +02:00
|
|
|
import logging
|
2018-10-25 11:56:46 +02:00
|
|
|
import zipfile
|
2019-04-27 13:05:36 +02:00
|
|
|
import tarfile
|
2018-06-22 21:16:55 +02:00
|
|
|
|
2018-10-22 13:42:04 +02:00
|
|
|
from libmat2 import pdf, images, audio, office, parser_factory, torrent
|
2019-04-27 13:05:36 +02:00
|
|
|
from libmat2 import harmless, video, web, archive
|
2018-06-22 21:16:55 +02:00
|
|
|
|
2018-10-02 15:26:13 +02:00
|
|
|
# No need to logging messages, should something go wrong,
|
|
|
|
# the testsuite _will_ fail.
|
|
|
|
logger = logging.getLogger()
|
|
|
|
logger.setLevel(logging.FATAL)
|
|
|
|
|
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):
|
|
|
|
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())
|
2019-02-07 21:58:10 +01:00
|
|
|
self.assertTrue(parser.get_meta())
|
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-10-01 22:26:35 +02:00
|
|
|
class TestWrongContentTypesFileOffice(unittest.TestCase):
|
|
|
|
def test_office_incomplete(self):
|
2018-09-30 19:52:35 +02:00
|
|
|
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-10-01 22:26:35 +02:00
|
|
|
def test_office_broken(self):
|
|
|
|
shutil.copy('./tests/data/broken_xml_content_types.docx', './tests/data/clean.docx')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
office.MSOfficeParser('./tests/data/clean.docx')
|
|
|
|
os.remove('./tests/data/clean.docx')
|
|
|
|
|
|
|
|
def test_office_absent(self):
|
|
|
|
shutil.copy('./tests/data/no_content_types.docx', './tests/data/clean.docx')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
office.MSOfficeParser('./tests/data/clean.docx')
|
|
|
|
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')
|
2018-10-22 13:42:04 +02:00
|
|
|
|
2018-10-23 16:14:21 +02:00
|
|
|
def test_png_lightweight(self):
|
|
|
|
return
|
|
|
|
shutil.copy('./tests/data/dirty.torrent', './tests/data/clean.png')
|
|
|
|
p = images.PNGParser('./tests/data/clean.png')
|
|
|
|
self.assertTrue(p.remove_all())
|
|
|
|
os.remove('./tests/data/clean.png')
|
|
|
|
|
2018-10-22 13:42:04 +02:00
|
|
|
def test_avi(self):
|
2018-10-22 13:55:09 +02:00
|
|
|
try:
|
|
|
|
video._get_ffmpeg_path()
|
|
|
|
except RuntimeError:
|
|
|
|
raise unittest.SkipTest
|
|
|
|
|
2018-10-22 13:42:04 +02:00
|
|
|
shutil.copy('./tests/data/dirty.torrent', './tests/data/clean.avi')
|
|
|
|
p = video.AVIParser('./tests/data/clean.avi')
|
|
|
|
self.assertFalse(p.remove_all())
|
|
|
|
os.remove('./tests/data/clean.avi')
|
2018-10-22 19:12:39 +02:00
|
|
|
|
|
|
|
def test_avi_injection(self):
|
|
|
|
try:
|
|
|
|
video._get_ffmpeg_path()
|
|
|
|
except RuntimeError:
|
|
|
|
raise unittest.SkipTest
|
|
|
|
|
|
|
|
shutil.copy('./tests/data/dirty.torrent', './tests/data/--output.avi')
|
|
|
|
p = video.AVIParser('./tests/data/--output.avi')
|
|
|
|
self.assertFalse(p.remove_all())
|
|
|
|
os.remove('./tests/data/--output.avi')
|
2018-10-25 11:56:46 +02:00
|
|
|
|
|
|
|
def test_zip(self):
|
2019-02-21 01:28:11 +01:00
|
|
|
with zipfile.ZipFile('./tests/data/clean.zip', 'w') as zout:
|
2018-10-25 11:56:46 +02:00
|
|
|
zout.write('./tests/data/dirty.flac')
|
|
|
|
zout.write('./tests/data/dirty.docx')
|
|
|
|
zout.write('./tests/data/dirty.jpg')
|
|
|
|
zout.write('./tests/data/embedded_corrupted.docx')
|
2019-02-21 01:28:11 +01:00
|
|
|
p, mimetype = parser_factory.get_parser('./tests/data/clean.zip')
|
2018-10-25 11:56:46 +02:00
|
|
|
self.assertEqual(mimetype, 'application/zip')
|
|
|
|
meta = p.get_meta()
|
|
|
|
self.assertEqual(meta['tests/data/dirty.flac']['comments'], 'Thank you for using MAT !')
|
|
|
|
self.assertEqual(meta['tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')
|
|
|
|
self.assertFalse(p.remove_all())
|
2019-02-21 01:28:11 +01:00
|
|
|
os.remove('./tests/data/clean.zip')
|
2019-02-08 00:26:47 +01:00
|
|
|
|
|
|
|
def test_html(self):
|
|
|
|
shutil.copy('./tests/data/dirty.html', './tests/data/clean.html')
|
|
|
|
with open('./tests/data/clean.html', 'a') as f:
|
|
|
|
f.write('<open>but not</closed>')
|
|
|
|
with self.assertRaises(ValueError):
|
2019-02-21 01:28:11 +01:00
|
|
|
web.HTMLParser('./tests/data/clean.html')
|
2019-02-08 00:26:47 +01:00
|
|
|
os.remove('./tests/data/clean.html')
|
|
|
|
|
|
|
|
# Yes, we're able to deal with malformed html :/
|
|
|
|
shutil.copy('./tests/data/dirty.html', './tests/data/clean.html')
|
|
|
|
with open('./tests/data/clean.html', 'a') as f:
|
|
|
|
f.write('<meta name=\'this" is="weird"/>')
|
2019-02-21 01:28:11 +01:00
|
|
|
p = web.HTMLParser('./tests/data/clean.html')
|
2019-02-08 00:26:47 +01:00
|
|
|
self.assertTrue(p.remove_all())
|
2019-02-21 01:28:11 +01:00
|
|
|
p = web.HTMLParser('./tests/data/clean.cleaned.html')
|
2019-02-08 00:26:47 +01:00
|
|
|
self.assertEqual(p.get_meta(), {})
|
|
|
|
os.remove('./tests/data/clean.html')
|
|
|
|
os.remove('./tests/data/clean.cleaned.html')
|
|
|
|
|
|
|
|
with open('./tests/data/clean.html', 'w') as f:
|
2019-02-27 23:04:38 +01:00
|
|
|
f.write('</meta>')
|
2019-02-08 00:26:47 +01:00
|
|
|
with self.assertRaises(ValueError):
|
2019-02-21 01:28:11 +01:00
|
|
|
web.HTMLParser('./tests/data/clean.html')
|
2019-02-08 00:26:47 +01:00
|
|
|
os.remove('./tests/data/clean.html')
|
|
|
|
|
|
|
|
with open('./tests/data/clean.html', 'w') as f:
|
2019-02-27 23:04:38 +01:00
|
|
|
f.write('<meta><a>test</a><set/></meta><title></title><meta>')
|
2019-02-21 01:28:11 +01:00
|
|
|
p = web.HTMLParser('./tests/data/clean.html')
|
2019-02-08 00:26:47 +01:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
p.get_meta()
|
2019-02-21 01:28:11 +01:00
|
|
|
p = web.HTMLParser('./tests/data/clean.html')
|
2019-02-08 00:26:47 +01:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
p.remove_all()
|
|
|
|
os.remove('./tests/data/clean.html')
|
|
|
|
|
2019-02-21 01:28:11 +01:00
|
|
|
with open('./tests/data/clean.html', 'w') as f:
|
|
|
|
f.write('<doctitle><br/></doctitle><br/><notclosed>')
|
|
|
|
p = web.HTMLParser('./tests/data/clean.html')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
p.get_meta()
|
|
|
|
p = web.HTMLParser('./tests/data/clean.html')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
p.remove_all()
|
|
|
|
os.remove('./tests/data/clean.html')
|
|
|
|
|
|
|
|
def test_epub(self):
|
|
|
|
with zipfile.ZipFile('./tests/data/clean.epub', 'w') as zout:
|
|
|
|
zout.write('./tests/data/dirty.jpg', 'OEBPS/content.opf')
|
|
|
|
p, mimetype = parser_factory.get_parser('./tests/data/clean.epub')
|
|
|
|
self.assertEqual(mimetype, 'application/epub+zip')
|
|
|
|
meta = p.get_meta()
|
|
|
|
self.assertEqual(meta['OEBPS/content.opf']['OEBPS/content.opf'],
|
|
|
|
'harmful content')
|
|
|
|
|
|
|
|
self.assertFalse(p.remove_all())
|
|
|
|
os.remove('./tests/data/clean.epub')
|
2019-02-08 00:26:47 +01:00
|
|
|
|
2019-04-27 13:05:36 +02:00
|
|
|
def test_tar(self):
|
2019-04-27 22:33:54 +02:00
|
|
|
with tarfile.TarFile.open('./tests/data/clean.tar', 'w') as zout:
|
2019-04-27 13:05:36 +02:00
|
|
|
zout.add('./tests/data/dirty.flac')
|
|
|
|
zout.add('./tests/data/dirty.docx')
|
|
|
|
zout.add('./tests/data/dirty.jpg')
|
|
|
|
zout.add('./tests/data/embedded_corrupted.docx')
|
|
|
|
tarinfo = tarfile.TarInfo(name='./tests/data/dirty.png')
|
|
|
|
tarinfo.mtime = time.time()
|
|
|
|
tarinfo.uid = 1337
|
|
|
|
tarinfo.gid = 1338
|
2019-04-27 22:33:54 +02:00
|
|
|
tarinfo.size = os.stat('./tests/data/dirty.png').st_size
|
2019-04-27 13:05:36 +02:00
|
|
|
with open('./tests/data/dirty.png', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo, f)
|
|
|
|
p, mimetype = parser_factory.get_parser('./tests/data/clean.tar')
|
|
|
|
self.assertEqual(mimetype, 'application/x-tar')
|
|
|
|
meta = p.get_meta()
|
|
|
|
self.assertEqual(meta['./tests/data/dirty.flac']['comments'], 'Thank you for using MAT !')
|
|
|
|
self.assertEqual(meta['./tests/data/dirty.docx']['word/media/image1.png']['Comment'], 'This is a comment, be careful!')
|
|
|
|
self.assertFalse(p.remove_all())
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.tar')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
2019-04-27 22:33:54 +02:00
|
|
|
|
|
|
|
class TestReadOnlyArchiveMembers(unittest.TestCase):
|
|
|
|
def test_onlymember_tar(self):
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('./tests/data/dirty.png')
|
|
|
|
tarinfo = tarfile.TarInfo('./tests/data/dirty.jpg')
|
|
|
|
tarinfo.mtime = time.time()
|
|
|
|
tarinfo.uid = 1337
|
2019-04-28 19:25:45 +02:00
|
|
|
tarinfo.gid = 0
|
2019-04-27 22:33:54 +02:00
|
|
|
tarinfo.mode = 0o000
|
|
|
|
tarinfo.size = os.stat('./tests/data/dirty.jpg').st_size
|
|
|
|
with open('./tests/data/dirty.jpg', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo=tarinfo, fileobj=f)
|
|
|
|
p, mimetype = parser_factory.get_parser('./tests/data/clean.tar')
|
|
|
|
self.assertEqual(mimetype, 'application/x-tar')
|
|
|
|
meta = p.get_meta()
|
|
|
|
self.assertEqual(meta['./tests/data/dirty.jpg']['uid'], '1337')
|
|
|
|
self.assertTrue(p.remove_all())
|
|
|
|
|
|
|
|
p = archive.TarParser('./tests/data/clean.cleaned.tar')
|
|
|
|
self.assertEqual(p.get_meta(), {})
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.cleaned.tar')
|
|
|
|
|
2019-04-28 19:25:45 +02:00
|
|
|
|
|
|
|
class TestPathTraversalArchiveMembers(unittest.TestCase):
|
|
|
|
def test_tar_traversal(self):
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('./tests/data/dirty.png')
|
|
|
|
tarinfo = tarfile.TarInfo('./tests/data/dirty.jpg')
|
|
|
|
tarinfo.name = '../../../../../../../../../../tmp/mat2_test.png'
|
|
|
|
with open('./tests/data/dirty.jpg', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo=tarinfo, fileobj=f)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
|
|
|
|
def test_tar_absolute_path(self):
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('./tests/data/dirty.png')
|
|
|
|
tarinfo = tarfile.TarInfo('./tests/data/dirty.jpg')
|
|
|
|
tarinfo.name = '/etc/passwd'
|
|
|
|
with open('./tests/data/dirty.jpg', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo=tarinfo, fileobj=f)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
|
|
|
|
def test_tar_duplicate_file(self):
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
for _ in range(3):
|
|
|
|
zout.add('./tests/data/dirty.png')
|
|
|
|
tarinfo = tarfile.TarInfo('./tests/data/dirty.jpg')
|
|
|
|
with open('./tests/data/dirty.jpg', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo=tarinfo, fileobj=f)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
|
|
|
|
def test_tar_setuid(self):
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('./tests/data/dirty.png')
|
|
|
|
tarinfo = tarfile.TarInfo('./tests/data/dirty.jpg')
|
|
|
|
tarinfo.mode |= stat.S_ISUID
|
|
|
|
with open('./tests/data/dirty.jpg', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo=tarinfo, fileobj=f)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
|
|
|
|
def test_tar_setgid(self):
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('./tests/data/dirty.png')
|
|
|
|
tarinfo = tarfile.TarInfo('./tests/data/dirty.jpg')
|
|
|
|
tarinfo.mode |= stat.S_ISGID
|
|
|
|
with open('./tests/data/dirty.jpg', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo=tarinfo, fileobj=f)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
|
|
|
|
def test_tar_symlink_absolute(self):
|
|
|
|
os.symlink('/etc/passwd', './tests/data/symlink')
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('./tests/data/symlink')
|
|
|
|
tarinfo = tarfile.TarInfo('./tests/data/symlink')
|
|
|
|
tarinfo.linkname = '/etc/passwd'
|
|
|
|
tarinfo.type = tarfile.SYMTYPE
|
|
|
|
with open('./tests/data/dirty.jpg', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo=tarinfo, fileobj=f)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/symlink')
|
|
|
|
|
|
|
|
def test_tar_symlink_ok(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.png')
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('./tests/data/dirty.png')
|
|
|
|
t = tarfile.TarInfo('mydir')
|
|
|
|
t.type = tarfile.DIRTYPE
|
|
|
|
zout.addfile(t)
|
|
|
|
zout.add('./tests/data/clean.png')
|
|
|
|
t = tarfile.TarInfo('mylink')
|
|
|
|
t.type = tarfile.SYMTYPE
|
|
|
|
t.linkname = './tests/data/clean.png'
|
|
|
|
zout.addfile(t)
|
|
|
|
zout.add('./tests/data/dirty.jpg')
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.png')
|
|
|
|
|
|
|
|
def test_tar_symlink_relative(self):
|
|
|
|
os.symlink('../../../etc/passwd', './tests/data/symlink')
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('./tests/data/symlink')
|
|
|
|
tarinfo = tarfile.TarInfo('./tests/data/symlink')
|
|
|
|
with open('./tests/data/dirty.jpg', 'rb') as f:
|
|
|
|
zout.addfile(tarinfo=tarinfo, fileobj=f)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/symlink')
|
|
|
|
|
|
|
|
def test_tar_device_file(self):
|
|
|
|
with tarfile.open('./tests/data/clean.tar', 'w') as zout:
|
|
|
|
zout.add('/dev/null')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/clean.tar')
|
|
|
|
os.remove('./tests/data/clean.tar')
|
|
|
|
|
|
|
|
def test_tar_hardlink(self):
|
|
|
|
shutil.copy('./tests/data/dirty.png', './tests/data/clean.png')
|
|
|
|
os.link('./tests/data/clean.png', './tests/data/hardlink.png')
|
|
|
|
with tarfile.open('./tests/data/cleaner.tar', 'w') as zout:
|
|
|
|
zout.add('tests/data/clean.png')
|
|
|
|
zout.add('tests/data/hardlink.png')
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
archive.TarParser('./tests/data/cleaner.tar')
|
|
|
|
os.remove('./tests/data/cleaner.tar')
|
|
|
|
os.remove('./tests/data/clean.png')
|
|
|
|
os.remove('./tests/data/hardlink.png')
|