2019-11-30 09:57:18 +01:00
|
|
|
import sys
|
2019-05-09 21:08:52 +02:00
|
|
|
import random
|
2018-04-16 23:20:21 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
2019-10-13 11:54:47 +02:00
|
|
|
import stat
|
2018-04-04 00:22:00 +02:00
|
|
|
import subprocess
|
2018-04-16 23:20:21 +02:00
|
|
|
import unittest
|
2019-05-08 21:27:28 +02:00
|
|
|
import glob
|
|
|
|
|
|
|
|
from libmat2 import images, parser_factory
|
2018-10-04 16:14:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
mat2_binary = ['./mat2']
|
|
|
|
|
|
|
|
if 'MAT2_GLOBAL_PATH_TESTSUITE' in os.environ:
|
|
|
|
# Debian runs tests after installing the package
|
|
|
|
# https://0xacab.org/jvoisin/mat2/issues/16#note_153878
|
|
|
|
mat2_binary = ['/usr/bin/env', 'mat2']
|
2018-04-04 00:22:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestHelp(unittest.TestCase):
|
|
|
|
def test_help(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--help'], stdout=subprocess.PIPE)
|
2018-04-04 00:22:00 +02:00
|
|
|
stdout, _ = proc.communicate()
|
2019-12-23 23:10:39 +01:00
|
|
|
self.assertIn(b'mat2 [-h] [-V]', stdout)
|
|
|
|
self.assertIn(b'[--unknown-members policy]', stdout)
|
|
|
|
self.assertIn(b'[--inplace]', stdout)
|
|
|
|
self.assertIn(b'[--no-sandbox]', stdout)
|
|
|
|
self.assertIn(b' [-v] [-l]', stdout)
|
|
|
|
self.assertIn(b'[--check-dependencies]', stdout)
|
|
|
|
self.assertIn(b'[-L | -s]', stdout)
|
2020-11-13 17:30:47 +01:00
|
|
|
self.assertIn(b'[files ...]', stdout)
|
2018-04-04 00:37:55 +02:00
|
|
|
|
2018-04-04 23:14:43 +02:00
|
|
|
def test_no_arg(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary, stdout=subprocess.PIPE)
|
2018-04-04 23:14:43 +02:00
|
|
|
stdout, _ = proc.communicate()
|
2019-12-23 23:10:39 +01:00
|
|
|
self.assertIn(b'mat2 [-h] [-V]', stdout)
|
|
|
|
self.assertIn(b'[--unknown-members policy]', stdout)
|
|
|
|
self.assertIn(b'[--inplace]', stdout)
|
|
|
|
self.assertIn(b'[--no-sandbox]', stdout)
|
2019-10-13 01:13:49 +02:00
|
|
|
self.assertIn(b' [-v] [-l] [--check-dependencies] [-L | -s]', stdout)
|
2020-11-13 17:30:47 +01:00
|
|
|
self.assertIn(b'[files ...]', stdout)
|
2018-05-14 22:44:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestVersion(unittest.TestCase):
|
|
|
|
def test_version(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--version'], stdout=subprocess.PIPE)
|
2018-05-14 22:44:31 +02:00
|
|
|
stdout, _ = proc.communicate()
|
2019-11-28 03:15:20 +01:00
|
|
|
self.assertTrue(stdout.startswith(b'mat2 '))
|
2018-05-14 22:44:31 +02:00
|
|
|
|
2019-10-13 01:13:49 +02:00
|
|
|
|
2018-07-10 20:49:54 +02:00
|
|
|
class TestDependencies(unittest.TestCase):
|
|
|
|
def test_dependencies(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--check-dependencies'], stdout=subprocess.PIPE)
|
2018-07-10 20:49:54 +02:00
|
|
|
stdout, _ = proc.communicate()
|
2019-11-28 03:15:20 +01:00
|
|
|
self.assertTrue(b'mat2' in stdout)
|
2018-04-04 23:14:43 +02:00
|
|
|
|
2019-10-13 01:13:49 +02:00
|
|
|
|
2018-04-29 22:55:26 +02:00
|
|
|
class TestReturnValue(unittest.TestCase):
|
|
|
|
def test_nonzero(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
ret = subprocess.call(mat2_binary + ['mat2'], stdout=subprocess.DEVNULL)
|
2018-04-29 22:55:26 +02:00
|
|
|
self.assertEqual(255, ret)
|
|
|
|
|
2018-10-04 16:14:38 +02:00
|
|
|
ret = subprocess.call(mat2_binary + ['--whololo'], stderr=subprocess.DEVNULL)
|
2018-04-29 22:55:26 +02:00
|
|
|
self.assertEqual(2, ret)
|
|
|
|
|
|
|
|
def test_zero(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
ret = subprocess.call(mat2_binary, stdout=subprocess.DEVNULL)
|
2018-04-29 22:55:26 +02:00
|
|
|
self.assertEqual(0, ret)
|
|
|
|
|
2018-10-04 16:14:38 +02:00
|
|
|
ret = subprocess.call(mat2_binary + ['--show', 'mat2'], stdout=subprocess.DEVNULL)
|
2018-04-29 22:55:26 +02:00
|
|
|
self.assertEqual(0, ret)
|
|
|
|
|
|
|
|
|
2018-07-30 22:54:41 +02:00
|
|
|
class TestCleanFolder(unittest.TestCase):
|
|
|
|
def test_jpg(self):
|
2018-09-06 11:32:29 +02:00
|
|
|
try:
|
|
|
|
os.mkdir('./tests/data/folder/')
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
2018-07-30 22:54:41 +02:00
|
|
|
shutil.copy('./tests/data/dirty.jpg', './tests/data/folder/clean1.jpg')
|
|
|
|
shutil.copy('./tests/data/dirty.jpg', './tests/data/folder/clean2.jpg')
|
|
|
|
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/folder/'],
|
2018-07-30 22:54:41 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'Comment: Created with GIMP', stdout)
|
|
|
|
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['./tests/data/folder/'],
|
2018-07-30 22:54:41 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
|
|
|
|
os.remove('./tests/data/folder/clean1.jpg')
|
|
|
|
os.remove('./tests/data/folder/clean2.jpg')
|
|
|
|
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/folder/'],
|
2018-07-30 22:54:41 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertNotIn(b'Comment: Created with GIMP', stdout)
|
2018-10-05 12:35:35 +02:00
|
|
|
self.assertIn(b'No metadata found', stdout)
|
2018-07-30 22:54:41 +02:00
|
|
|
|
|
|
|
shutil.rmtree('./tests/data/folder/')
|
|
|
|
|
|
|
|
|
2018-04-16 23:20:21 +02:00
|
|
|
class TestCleanMeta(unittest.TestCase):
|
|
|
|
def test_jpg(self):
|
|
|
|
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
|
|
|
|
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/clean.jpg'],
|
2018-04-16 23:20:21 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'Comment: Created with GIMP', stdout)
|
|
|
|
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['./tests/data/clean.jpg'],
|
2018-04-16 23:20:21 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/clean.cleaned.jpg'],
|
2018-04-16 23:20:21 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertNotIn(b'Comment: Created with GIMP', stdout)
|
|
|
|
|
|
|
|
os.remove('./tests/data/clean.jpg')
|
|
|
|
|
2019-10-13 01:13:49 +02:00
|
|
|
def test_jpg_nosandbox(self):
|
|
|
|
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
|
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', '--no-sandbox', './tests/data/clean.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'Comment: Created with GIMP', stdout)
|
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['./tests/data/clean.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/clean.cleaned.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertNotIn(b'Comment: Created with GIMP', stdout)
|
|
|
|
|
|
|
|
os.remove('./tests/data/clean.jpg')
|
2019-10-13 11:54:47 +02:00
|
|
|
os.remove('./tests/data/clean.cleaned.jpg')
|
|
|
|
|
|
|
|
|
|
|
|
class TestCopyPermissions(unittest.TestCase):
|
|
|
|
def test_jpg_777(self):
|
|
|
|
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
|
|
|
|
os.chmod('./tests/data/clean.jpg', 0o777)
|
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/clean.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'Comment: Created with GIMP', stdout)
|
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['./tests/data/clean.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/clean.cleaned.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertNotIn(b'Comment: Created with GIMP', stdout)
|
|
|
|
|
|
|
|
permissions = os.stat('./tests/data/clean.cleaned.jpg')[stat.ST_MODE]
|
|
|
|
self.assertEqual(permissions, 0o100777)
|
|
|
|
|
|
|
|
os.remove('./tests/data/clean.jpg')
|
|
|
|
os.remove('./tests/data/clean.cleaned.jpg')
|
2019-10-13 01:13:49 +02:00
|
|
|
|
2018-04-16 23:20:21 +02:00
|
|
|
|
2018-06-04 22:54:01 +02:00
|
|
|
class TestIsSupported(unittest.TestCase):
|
|
|
|
def test_pdf(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.pdf'],
|
2018-06-04 22:54:01 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertNotIn(b"isn't supported", stdout)
|
|
|
|
|
2018-04-04 00:37:55 +02:00
|
|
|
class TestGetMeta(unittest.TestCase):
|
2018-10-18 20:29:50 +02:00
|
|
|
maxDiff = None
|
|
|
|
|
2018-04-04 00:37:55 +02:00
|
|
|
def test_pdf(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.pdf'],
|
2018-04-04 00:37:55 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
2018-11-07 21:54:34 +01:00
|
|
|
self.assertIn(b'Producer: pdfTeX-1.40.14', stdout)
|
2018-04-04 00:37:55 +02:00
|
|
|
|
|
|
|
def test_png(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.png'],
|
2018-04-04 00:37:55 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'Comment: This is a comment, be careful!', stdout)
|
|
|
|
|
|
|
|
def test_jpg(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.jpg'],
|
2018-04-04 00:37:55 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'Comment: Created with GIMP', stdout)
|
|
|
|
|
|
|
|
def test_docx(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.docx'],
|
2018-04-04 00:37:55 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'Application: LibreOffice/5.4.5.1$Linux_X86_64', stdout)
|
|
|
|
self.assertIn(b'creator: julien voisin', stdout)
|
|
|
|
self.assertIn(b'revision: 1', stdout)
|
|
|
|
|
|
|
|
def test_odt(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.odt'],
|
2018-04-04 00:37:55 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'generator: LibreOffice/3.3$Unix', stdout)
|
|
|
|
self.assertIn(b'creator: jvoisin', stdout)
|
|
|
|
self.assertIn(b'date_time: 2011-07-26 02:40:16', stdout)
|
2018-04-04 21:59:46 +02:00
|
|
|
|
|
|
|
def test_mp3(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.mp3'],
|
2018-04-04 21:59:46 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'TALB: harmfull', stdout)
|
|
|
|
self.assertIn(b'COMM::: Thank you for using MAT !', stdout)
|
|
|
|
|
|
|
|
def test_flac(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.flac'],
|
2018-10-14 10:59:50 +02:00
|
|
|
stdout=subprocess.PIPE, bufsize=0)
|
2018-04-04 21:59:46 +02:00
|
|
|
stdout, _ = proc.communicate()
|
2018-10-18 20:29:50 +02:00
|
|
|
self.assertIn(b'comments: Thank you for using MAT !', stdout)
|
2018-04-04 21:59:46 +02:00
|
|
|
self.assertIn(b'genre: Python', stdout)
|
|
|
|
self.assertIn(b'title: I am so', stdout)
|
|
|
|
|
|
|
|
def test_ogg(self):
|
2018-10-04 16:14:38 +02:00
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/dirty.ogg'],
|
2018-04-04 21:59:46 +02:00
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'comments: Thank you for using MAT !', stdout)
|
|
|
|
self.assertIn(b'genre: Python', stdout)
|
|
|
|
self.assertIn(b'i am a : various comment', stdout)
|
|
|
|
self.assertIn(b'artist: jvoisin', stdout)
|
2018-11-07 22:13:36 +01:00
|
|
|
|
2019-10-13 01:13:49 +02:00
|
|
|
|
2018-11-07 22:13:36 +01:00
|
|
|
class TestControlCharInjection(unittest.TestCase):
|
|
|
|
def test_jpg(self):
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/control_chars.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b'Comment: GQ\n', stdout)
|
2019-05-08 21:27:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestCommandLineParallel(unittest.TestCase):
|
|
|
|
iterations = 24
|
|
|
|
|
|
|
|
def test_same(self):
|
|
|
|
for i in range(self.iterations):
|
|
|
|
shutil.copy('./tests/data/dirty.jpg', './tests/data/dirty_%d.jpg' % i)
|
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['./tests/data/dirty_%d.jpg' % i for i in range(self.iterations)],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
|
|
|
|
for i in range(self.iterations):
|
|
|
|
path = './tests/data/dirty_%d.jpg' % i
|
|
|
|
p = images.JPGParser('./tests/data/dirty_%d.cleaned.jpg' % i)
|
|
|
|
self.assertEqual(p.get_meta(), {})
|
|
|
|
os.remove('./tests/data/dirty_%d.cleaned.jpg' % i)
|
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
def test_different(self):
|
2019-11-30 09:57:18 +01:00
|
|
|
src = './tests/data/'
|
|
|
|
dst = './tests/data/parallel'
|
2022-03-29 22:13:33 +02:00
|
|
|
shutil.copytree(src, dst)
|
2019-05-08 21:27:28 +02:00
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + glob.glob('./tests/data/parallel/dirty.*'),
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
|
|
|
|
for i in glob.glob('./test/data/parallel/dirty.cleaned.*'):
|
|
|
|
p, mime = parser_factory.get_parser(i)
|
|
|
|
self.assertIsNotNone(mime)
|
|
|
|
self.assertIsNotNone(p)
|
|
|
|
p = parser_factory.get_parser(p.output_filename)
|
|
|
|
self.assertEqual(p.get_meta(), {})
|
2022-03-29 22:13:33 +02:00
|
|
|
shutil.rmtree('./tests/data/parallel/')
|
2019-05-08 21:27:28 +02:00
|
|
|
|
2019-05-09 21:08:52 +02:00
|
|
|
def test_faulty(self):
|
|
|
|
for i in range(self.iterations):
|
|
|
|
shutil.copy('./tests/data/dirty.jpg', './tests/data/dirty_%d.jpg' % i)
|
|
|
|
shutil.copy('./tests/data/dirty.torrent', './tests/data/dirty_%d.docx' % i)
|
|
|
|
|
|
|
|
to_process = ['./tests/data/dirty_%d.jpg' % i for i in range(self.iterations)]
|
|
|
|
to_process.extend(['./tests/data/dirty_%d.docx' % i for i in range(self.iterations)])
|
|
|
|
random.shuffle(to_process)
|
|
|
|
proc = subprocess.Popen(mat2_binary + to_process,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
|
|
|
|
for i in range(self.iterations):
|
|
|
|
path = './tests/data/dirty_%d.jpg' % i
|
|
|
|
p = images.JPGParser('./tests/data/dirty_%d.cleaned.jpg' % i)
|
|
|
|
self.assertEqual(p.get_meta(), {})
|
|
|
|
os.remove('./tests/data/dirty_%d.cleaned.jpg' % i)
|
|
|
|
os.remove(path)
|
|
|
|
os.remove('./tests/data/dirty_%d.docx' % i)
|
2019-08-31 19:31:08 +02:00
|
|
|
|
2019-10-13 01:13:49 +02:00
|
|
|
|
2019-08-31 19:31:08 +02:00
|
|
|
class TestInplaceCleaning(unittest.TestCase):
|
|
|
|
def test_cleaning(self):
|
|
|
|
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['--inplace', './tests/data/clean.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['--show', './tests/data/clean.jpg'],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
self.assertIn(b' No metadata found in ./tests/data/clean.jpg.\n', stdout)
|
|
|
|
os.remove('./tests/data/clean.jpg')
|
|
|
|
|
|
|
|
def test_cleaning_multiple_one_fails(self):
|
|
|
|
files = ['./tests/data/clean_%d.jpg' % i for i in range(9)]
|
|
|
|
for f in files:
|
|
|
|
shutil.copy('./tests/data/dirty.jpg', f)
|
|
|
|
shutil.copy('./tests/data/dirty.torrent', './tests/data/clean_9.jpg')
|
|
|
|
|
|
|
|
proc = subprocess.Popen(mat2_binary + ['--inplace'] + files,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
stdout, _ = proc.communicate()
|
|
|
|
|
|
|
|
for f in files:
|
|
|
|
p = images.JPGParser(f)
|
|
|
|
meta = p.get_meta()
|
|
|
|
self.assertEqual(meta, {})
|
|
|
|
|
|
|
|
for i in range(10):
|
|
|
|
os.remove('./tests/data/clean_%d.jpg' % i)
|
|
|
|
|