aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjvoisin <julien.voisin@dustri.org>2020-11-23 19:50:46 +0100
committerjvoisin <julien.voisin@dustri.org>2020-11-30 18:52:07 +0100
commit61dce89fbd7759a9d2c19c46930030fd4467374f (patch)
tree80ad1da86db63507f4b35acf205a5d2386247403
parent88b7ec2c48a63b158ee69b14dadc506f3359df5f (diff)
downloadexternal_mat2-61dce89fbd7759a9d2c19c46930030fd4467374f.tar.gz
external_mat2-61dce89fbd7759a9d2c19c46930030fd4467374f.tar.bz2
external_mat2-61dce89fbd7759a9d2c19c46930030fd4467374f.zip
Raise a ValueError explicitly
-rw-r--r--libmat2/parser_factory.py13
-rwxr-xr-xmat212
-rw-r--r--tests/test_corrupted_files.py26
3 files changed, 30 insertions, 21 deletions
diff --git a/libmat2/parser_factory.py b/libmat2/parser_factory.py
index 23529db..842f0d7 100644
--- a/libmat2/parser_factory.py
+++ b/libmat2/parser_factory.py
@@ -40,7 +40,10 @@ def _get_parsers() -> List[T]:
def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]:
- """ Return the appropriate parser for a given filename. """
+ """ Return the appropriate parser for a given filename.
+
+ :raises ValueError: Raised if the instantiation of the parser went wrong.
+ """
mtype, _ = mimetypes.guess_type(filename)
_, extension = os.path.splitext(filename)
@@ -53,10 +56,6 @@ def get_parser(filename: str) -> Tuple[Optional[T], Optional[str]]:
for parser_class in _get_parsers(): # type: ignore
if mtype in parser_class.mimetypes:
- try:
- return parser_class(filename), mtype
- except ValueError as e:
- logging.info("Got an exception when trying to instantiate "
- "%s for %s: %s", parser_class, filename, e)
- return None, mtype
+ # This instantiation might raise a ValueError on malformed files
+ return parser_class(filename), mtype
return None, mtype
diff --git a/mat2 b/mat2
index 5134e77..f0d0539 100755
--- a/mat2
+++ b/mat2
@@ -85,7 +85,11 @@ def show_meta(filename: str, sandbox: bool):
if not __check_file(filename):
return
- p, mtype = parser_factory.get_parser(filename) # type: ignore
+ try:
+ p, mtype = parser_factory.get_parser(filename) # type: ignore
+ except ValueError as e:
+ print("[-] something went wrong when processing %s: %s" % (filename, e))
+ return
if p is None:
print("[-] %s's format (%s) is not supported" % (filename, mtype))
return
@@ -126,7 +130,11 @@ def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool
if not __check_file(filename, mode):
return False
- p, mtype = parser_factory.get_parser(filename) # type: ignore
+ try:
+ p, mtype = parser_factory.get_parser(filename) # type: ignore
+ except ValueError as e:
+ print("[-] something went wrong when cleaning %s: %s" % (filename, e))
+ return False
if p is None:
print("[-] %s's format (%s) is not supported" % (filename, mtype))
return False
diff --git a/tests/test_corrupted_files.py b/tests/test_corrupted_files.py
index 8a8cffe..2adf42e 100644
--- a/tests/test_corrupted_files.py
+++ b/tests/test_corrupted_files.py
@@ -65,8 +65,10 @@ class TestCorruptedEmbedded(unittest.TestCase):
def test_docx(self):
shutil.copy('./tests/data/embedded_corrupted.docx', './tests/data/clean.docx')
parser, _ = parser_factory.get_parser('./tests/data/clean.docx')
- self.assertFalse(parser.remove_all())
- self.assertIsNotNone(parser.get_meta())
+ with self.assertRaises(ValueError):
+ parser.remove_all()
+ with self.assertRaises(ValueError):
+ self.assertIsNotNone(parser.get_meta())
os.remove('./tests/data/clean.docx')
def test_odt(self):
@@ -120,8 +122,8 @@ class TestCorruptedFiles(unittest.TestCase):
def test_png2(self):
shutil.copy('./tests/test_libmat2.py', './tests/clean.png')
- parser, _ = parser_factory.get_parser('./tests/clean.png')
- self.assertIsNone(parser)
+ with self.assertRaises(ValueError):
+ parser_factory.get_parser('./tests/clean.png')
os.remove('./tests/clean.png')
def test_torrent(self):
@@ -237,10 +239,10 @@ class TestCorruptedFiles(unittest.TestCase):
zout.write('./tests/data/embedded_corrupted.docx')
p, mimetype = parser_factory.get_parser('./tests/data/clean.zip')
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())
+ with self.assertRaises(ValueError):
+ p.get_meta()
+ with self.assertRaises(ValueError):
+ self.assertFalse(p.remove_all())
os.remove('./tests/data/clean.zip')
def test_html(self):
@@ -315,10 +317,10 @@ class TestCorruptedFiles(unittest.TestCase):
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())
+ with self.assertRaises(ValueError):
+ p.get_meta()
+ with self.assertRaises(ValueError):
+ self.assertFalse(p.remove_all())
os.remove('./tests/data/clean.tar')
shutil.copy('./tests/data/dirty.png', './tests/data/clean.tar')