Commit 36c963ef authored by jvoisin's avatar jvoisin

Fix https://labs.riseup.net/code/issues/6416

This commit fix the "Does not inform the user when files where lost
while cleaning an archive" (for the CLI) bug, and adds a test for it.
parent 17344900
......@@ -12,6 +12,7 @@ import hachoir_core
from MAT import mat
from MAT import strippers
from MAT import archive
def parse():
......@@ -59,7 +60,7 @@ def display_version(*_):
sys.exit(0)
def list_meta(class_file, filename, force):
def list_meta(class_file, filename, force, add2archive):
'''
Print all the metadata of 'filename' on stdout
'''
......@@ -74,7 +75,7 @@ def list_meta(class_file, filename, force):
print('\t' + key + ' : ' + str(value))
def is_clean(class_file, filename, force):
def is_clean(class_file, filename, force, add2archive):
'''
Say if 'filename' is clean or not
'''
......@@ -84,21 +85,30 @@ def is_clean(class_file, filename, force):
print('[+] %s is not clean' % filename)
def clean_meta(class_file, filename, force):
def clean_meta(class_file, filename, force, add2archive):
'''
Clean the file 'filename'
'''
if not class_file.is_writable:
print('[-] %s is not writable' % filename)
return
print('[+] Cleaning %s' % filename)
print('[*] Cleaning %s' % filename)
if not force and class_file.is_clean():
print('%s is already clean' % filename)
print('[+] %s is already clean' % filename)
elif not add2archive:
is_archive = isinstance(class_file, archive.GenericArchiveStripper)
is_terminal = isinstance(class_file, archive.TerminalZipStripper)
if is_archive and not is_terminal:
unsupported_list = class_file.list_unsupported()
if type(unsupported_list) == list and unsupported_list:
print('[-] Can not clean: %s.'\
'It contains unsupported filetypes:' % filename)
for i in unsupported_list:
print('- %s' % i)
elif class_file.remove_all():
print('[+] %s cleaned !' % filename)
else:
if class_file.remove_all():
print('%s cleaned !' % filename)
else:
print('Unable to clean %s', filename)
print('[-] Unable to clean %s', filename)
def list_supported():
......@@ -142,7 +152,7 @@ def main():
class_file = mat.create_class_file(filename, args.backup,
add2archive=args.add2archive, low_pdf_quality=args.low_pdf_quality)
if class_file:
func(class_file, filename, args.force)
func(class_file, filename, args.force, args.add2archive)
else:
print('Unable to process %s' % filename)
......
......@@ -5,9 +5,11 @@
Unit test for the CLI interface
'''
import os
import unittest
import subprocess
import sys
import tarfile
sys.path.append('..')
from MAT import mat
......@@ -21,14 +23,14 @@ class TestRemovecli(test.MATTest):
def test_remove(self):
'''make sure that the cli remove all compromizing meta'''
for _, dirty in self.file_list:
subprocess.call(['../mat', dirty])
subprocess.call(['../mat', '--add2archive', dirty])
current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
self.assertTrue(current_file.is_clean())
def test_remove_empty(self):
'''Test removal with clean files'''
'''Test removal with clean files\n'''
for clean, _ in self.file_list:
subprocess.call(['../mat', clean])
subprocess.call(['../mat', '--add2archive', clean])
current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True)
self.assertTrue(current_file.is_clean())
......@@ -97,11 +99,24 @@ class TestFileAttributes(unittest.TestCase):
def test_empty(self):
''' test MAT's behaviour on empty file'''
proc = subprocess.Popen(['../mat', 'empty_file'],
stdout=subprocess.PIPE)
proc = subprocess.Popen(['../mat', 'empty_file'], stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
self.assertEqual(str(stdout).strip('\n'), 'Unable to process %s' % 'ilikecookies')
class TestUnsupported(test.MATTest):
def test_abort_unsupported(self):
''' test if the cli aborts on unsupported files
'''
tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
tar = tarfile.open(tarpath, "w")
for f in ('../mat.desktop', '../README.security', '../setup.py'):
tar.add(f, f[3:]) # trim '../'
tar.close()
proc = subprocess.Popen(['../mat', tarpath], stdout=subprocess.PIPE)
stdout, _ = proc.communicate()
self.assertTrue('It contains unsupported filetypes:'\
'\n- mat.desktop\n- README.security\n- setup.py\n'
in str(stdout))
def get_tests():
''' Return every clitests'''
......@@ -109,4 +124,5 @@ def get_tests():
suite.addTest(unittest.makeSuite(TestRemovecli))
suite.addTest(unittest.makeSuite(TestListcli))
suite.addTest(unittest.makeSuite(TestisCleancli))
suite.addTest(unittest.makeSuite(TestUnsupported))
return suite
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment