summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_corrupted_files.py88
-rw-r--r--tests/test_libmat2.py66
2 files changed, 88 insertions, 66 deletions
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
3import unittest
4import shutil
5import os
6
7from libmat2 import pdf, images, audio, office, parser_factory, torrent
8
9
10class 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
19class 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
28class 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
43class 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
51class 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
60class 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
110class TestGetMeta(unittest.TestCase): 44class TestGetMeta(unittest.TestCase):
111 def test_pdf(self): 45 def test_pdf(self):