diff options
| author | jvoisin | 2018-06-22 21:16:55 +0200 |
|---|---|---|
| committer | jvoisin | 2018-06-22 21:16:55 +0200 |
| commit | 74f2d50433a831787fe7ab791748c8efa854ada2 (patch) | |
| tree | 69e55499a8ce97f943c33dcedf0de7a2ade6981f | |
| parent | b4ef0c9622a0741bcfa0da1f65d9082251fb4107 (diff) | |
Split the testsuite a bit and add more tests
Diffstat (limited to '')
| -rw-r--r-- | libmat2/audio.py | 11 | ||||
| -rw-r--r-- | tests/test_corrupted_files.py | 88 | ||||
| -rw-r--r-- | tests/test_libmat2.py | 66 |
3 files changed, 99 insertions, 66 deletions
diff --git a/libmat2/audio.py b/libmat2/audio.py index 9b73031..a26f36f 100644 --- a/libmat2/audio.py +++ b/libmat2/audio.py | |||
| @@ -6,6 +6,17 @@ from . import abstract | |||
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | class MutagenParser(abstract.AbstractParser): | 8 | class MutagenParser(abstract.AbstractParser): |
| 9 | def __init__(self, filename): | ||
| 10 | super().__init__(filename) | ||
| 11 | try: | ||
| 12 | mutagen.File(self.filename) | ||
| 13 | except mutagen.flac.MutagenError: | ||
| 14 | raise ValueError | ||
| 15 | except mutagen.mp3.MutagenError: | ||
| 16 | raise ValueError | ||
| 17 | except mutagen.ogg.MutagenError: | ||
| 18 | raise ValueError | ||
| 19 | |||
| 9 | def get_meta(self): | 20 | def get_meta(self): |
| 10 | f = mutagen.File(self.filename) | 21 | f = mutagen.File(self.filename) |
| 11 | if f.tags: | 22 | if f.tags: |
diff --git a/tests/test_corrupted_files.py b/tests/test_corrupted_files.py new file mode 100644 index 0000000..b784b0e --- /dev/null +++ b/tests/test_corrupted_files.py | |||
| @@ -0,0 +1,88 @@ | |||
| 1 | #!/usr/bin/python3 | ||
| 2 | |||
| 3 | import unittest | ||
| 4 | import shutil | ||
| 5 | import os | ||
| 6 | |||
| 7 | from libmat2 import pdf, images, audio, office, parser_factory, torrent | ||
| 8 | |||
| 9 | |||
| 10 | class TestUnsupportedFiles(unittest.TestCase): | ||
| 11 | def test_pdf(self): | ||
| 12 | shutil.copy('./tests/test_libmat2.py', './tests/clean.py') | ||
| 13 | parser, mimetype = parser_factory.get_parser('./tests/data/clean.py') | ||
| 14 | self.assertEqual(mimetype, 'text/x-python') | ||
| 15 | self.assertEqual(parser, None) | ||
| 16 | os.remove('./tests/clean.py') | ||
| 17 | |||
| 18 | |||
| 19 | class TestExplicitelyUnsupportedFiles(unittest.TestCase): | ||
| 20 | def test_pdf(self): | ||
| 21 | shutil.copy('./tests/test_libmat2.py', './tests/clean.txt') | ||
| 22 | parser, mimetype = parser_factory.get_parser('./tests/data/clean.txt') | ||
| 23 | self.assertEqual(mimetype, 'text/plain') | ||
| 24 | self.assertEqual(parser, None) | ||
| 25 | os.remove('./tests/clean.txt') | ||
| 26 | |||
| 27 | |||
| 28 | class TestCorruptedFiles(unittest.TestCase): | ||
| 29 | def test_pdf(self): | ||
| 30 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.png') | ||
| 31 | with self.assertRaises(ValueError): | ||
| 32 | pdf.PDFParser('./tests/data/clean.png') | ||
| 33 | os.remove('./tests/data/clean.png') | ||
| 34 | |||
| 35 | def test_png(self): | ||
| 36 | shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf') | ||
| 37 | with self.assertRaises(ValueError): | ||
| 38 | images.PNGParser('./tests/data/clean.pdf') | ||
| 39 | os.remove('./tests/data/clean.pdf') | ||
| 40 | |||
| 41 | def test_png2(self): | ||
| 42 | shutil.copy('./tests/test_libmat2.py', './tests/clean.png') | ||
| 43 | parser, mimetype = parser_factory.get_parser('./tests/clean.png') | ||
| 44 | self.assertIsNone(parser) | ||
| 45 | os.remove('./tests/clean.png') | ||
| 46 | |||
| 47 | def test_torrent(self): | ||
| 48 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.torrent') | ||
| 49 | p = torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 50 | self.assertFalse(p.remove_all()) | ||
| 51 | expected = {'Unknown meta': 'Unable to parse torrent file "./tests/data/clean.torrent".'} | ||
| 52 | self.assertEqual(p.get_meta(), expected) | ||
| 53 | |||
| 54 | with open("./tests/data/clean.torrent", "a") as f: | ||
| 55 | f.write("trailing garbage") | ||
| 56 | p = torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 57 | self.assertEqual(p.get_meta(), expected) | ||
| 58 | os.remove('./tests/data/clean.torrent') | ||
| 59 | |||
| 60 | def test_odg(self): | ||
| 61 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.odg') | ||
| 62 | with self.assertRaises(ValueError): | ||
| 63 | office.LibreOfficeParser('./tests/data/clean.odg') | ||
| 64 | os.remove('./tests/data/clean.odg') | ||
| 65 | |||
| 66 | def test_bmp(self): | ||
| 67 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.bmp') | ||
| 68 | with self.assertRaises(ValueError): | ||
| 69 | images.BMPParser('./tests/data/clean.bmp') | ||
| 70 | os.remove('./tests/data/clean.bmp') | ||
| 71 | |||
| 72 | def test_docx(self): | ||
| 73 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.docx') | ||
| 74 | with self.assertRaises(ValueError): | ||
| 75 | office.MSOfficeParser('./tests/data/clean.docx') | ||
| 76 | os.remove('./tests/data/clean.docx') | ||
| 77 | |||
| 78 | def test_flac(self): | ||
| 79 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.flac') | ||
| 80 | with self.assertRaises(ValueError): | ||
| 81 | audio.FLACParser('./tests/data/clean.flac') | ||
| 82 | os.remove('./tests/data/clean.flac') | ||
| 83 | |||
| 84 | def test_mp3(self): | ||
| 85 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.mp3') | ||
| 86 | with self.assertRaises(ValueError): | ||
| 87 | audio.MP3Parser('./tests/data/clean.mp3') | ||
| 88 | os.remove('./tests/data/clean.mp3') | ||
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index 0df333d..b34e7a4 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -40,72 +40,6 @@ class TestUnsupportedEmbeddedFiles(unittest.TestCase): | |||
| 40 | self.assertFalse(p.remove_all()) | 40 | self.assertFalse(p.remove_all()) |
| 41 | os.remove('./tests/data/clean.docx') | 41 | os.remove('./tests/data/clean.docx') |
| 42 | 42 | ||
| 43 | class TestUnsupportedFiles(unittest.TestCase): | ||
| 44 | def test_pdf(self): | ||
| 45 | shutil.copy('./tests/test_libmat2.py', './tests/clean.py') | ||
| 46 | parser, mimetype = parser_factory.get_parser('./tests/data/clean.py') | ||
| 47 | self.assertEqual(mimetype, 'text/x-python') | ||
| 48 | self.assertEqual(parser, None) | ||
| 49 | os.remove('./tests/clean.py') | ||
| 50 | |||
| 51 | class TestExplicitelyUnsupportedFiles(unittest.TestCase): | ||
| 52 | def test_pdf(self): | ||
| 53 | shutil.copy('./tests/test_libmat2.py', './tests/clean.txt') | ||
| 54 | parser, mimetype = parser_factory.get_parser('./tests/data/clean.txt') | ||
| 55 | self.assertEqual(mimetype, 'text/plain') | ||
| 56 | self.assertEqual(parser, None) | ||
| 57 | os.remove('./tests/clean.txt') | ||
| 58 | |||
| 59 | |||
| 60 | class TestCorruptedFiles(unittest.TestCase): | ||
| 61 | def test_pdf(self): | ||
| 62 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.png') | ||
| 63 | with self.assertRaises(ValueError): | ||
| 64 | pdf.PDFParser('./tests/data/clean.png') | ||
| 65 | os.remove('./tests/data/clean.png') | ||
| 66 | |||
| 67 | def test_png(self): | ||
| 68 | shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf') | ||
| 69 | with self.assertRaises(ValueError): | ||
| 70 | images.PNGParser('./tests/data/clean.pdf') | ||
| 71 | os.remove('./tests/data/clean.pdf') | ||
| 72 | |||
| 73 | def test_png2(self): | ||
| 74 | shutil.copy('./tests/test_libmat2.py', './tests/clean.png') | ||
| 75 | parser, mimetype = parser_factory.get_parser('./tests/clean.png') | ||
| 76 | self.assertIsNone(parser) | ||
| 77 | os.remove('./tests/clean.png') | ||
| 78 | |||
| 79 | def test_torrent(self): | ||
| 80 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.torrent') | ||
| 81 | p = torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 82 | self.assertFalse(p.remove_all()) | ||
| 83 | expected = {'Unknown meta': 'Unable to parse torrent file "./tests/data/clean.torrent".'} | ||
| 84 | self.assertEqual(p.get_meta(), expected) | ||
| 85 | |||
| 86 | with open("./tests/data/clean.torrent", "a") as f: | ||
| 87 | f.write("trailing garbage") | ||
| 88 | p = torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 89 | self.assertEqual(p.get_meta(), expected) | ||
| 90 | os.remove('./tests/data/clean.torrent') | ||
| 91 | |||
| 92 | def test_odg(self): | ||
| 93 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.odg') | ||
| 94 | with self.assertRaises(ValueError): | ||
| 95 | office.LibreOfficeParser('./tests/data/clean.odg') | ||
| 96 | os.remove('./tests/data/clean.odg') | ||
| 97 | |||
| 98 | def test_bmp(self): | ||
| 99 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.bmp') | ||
| 100 | with self.assertRaises(ValueError): | ||
| 101 | p = images.BMPParser('./tests/data/clean.bmp') | ||
| 102 | os.remove('./tests/data/clean.bmp') | ||
| 103 | |||
| 104 | def test_docx(self): | ||
| 105 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.docx') | ||
| 106 | with self.assertRaises(ValueError): | ||
| 107 | p = office.MSOfficeParser('./tests/data/clean.docx') | ||
| 108 | os.remove('./tests/data/clean.docx') | ||
| 109 | 43 | ||
| 110 | class TestGetMeta(unittest.TestCase): | 44 | class TestGetMeta(unittest.TestCase): |
| 111 | def test_pdf(self): | 45 | def test_pdf(self): |
