diff options
Diffstat (limited to 'test/clitest.py')
| -rw-r--r-- | test/clitest.py | 178 |
1 files changed, 0 insertions, 178 deletions
diff --git a/test/clitest.py b/test/clitest.py deleted file mode 100644 index 884655f..0000000 --- a/test/clitest.py +++ /dev/null | |||
| @@ -1,178 +0,0 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -* | ||
| 3 | |||
| 4 | """ | ||
| 5 | Unit test for the CLI interface | ||
| 6 | """ | ||
| 7 | |||
| 8 | import os | ||
| 9 | import unittest | ||
| 10 | import subprocess | ||
| 11 | import sys | ||
| 12 | import tarfile | ||
| 13 | import stat | ||
| 14 | |||
| 15 | import test | ||
| 16 | from libmat import mat | ||
| 17 | |||
| 18 | |||
| 19 | class TestRemovecli(test.MATTest): | ||
| 20 | """ | ||
| 21 | test if cli correctly remove metadatas | ||
| 22 | """ | ||
| 23 | |||
| 24 | def test_remove(self): | ||
| 25 | """make sure that the cli remove all compromizing meta""" | ||
| 26 | for _, dirty in self.file_list: | ||
| 27 | subprocess.call(['mat', '--add2archive', dirty]) | ||
| 28 | current_file = mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) | ||
| 29 | self.assertTrue(current_file.is_clean()) | ||
| 30 | |||
| 31 | def test_remove_fileformat_specific_options(self): | ||
| 32 | """ test metadata removal with fileformat-specific options """ | ||
| 33 | for _, dirty in self.file_list: # can't be faster than that :/ | ||
| 34 | if dirty.endswith('pdf'): | ||
| 35 | subprocess.call(['mat', '--low-pdf-quality', dirty]) | ||
| 36 | current_file = mat.create_class_file(dirty, False, low_pdf_quality=True) | ||
| 37 | self.assertTrue(current_file.is_clean()) | ||
| 38 | |||
| 39 | def test_remove_empty(self): | ||
| 40 | """Test removal with clean files\n""" | ||
| 41 | for clean, _ in self.file_list: | ||
| 42 | subprocess.call(['mat', '--add2archive', clean]) | ||
| 43 | current_file = mat.create_class_file(clean, False, add2archive=True, low_pdf_quality=True) | ||
| 44 | self.assertTrue(current_file.is_clean()) | ||
| 45 | |||
| 46 | |||
| 47 | class TestListcli(test.MATTest): | ||
| 48 | """ | ||
| 49 | test if cli correctly display metadatas | ||
| 50 | """ | ||
| 51 | |||
| 52 | def test_list_clean(self): | ||
| 53 | """check if get_meta returns meta""" | ||
| 54 | for clean, _ in self.file_list: | ||
| 55 | proc = subprocess.Popen(['mat', '-d', clean], | ||
| 56 | stdout=subprocess.PIPE) | ||
| 57 | stdout, _ = proc.communicate() | ||
| 58 | self.assertEqual(str(stdout).strip('\n'), "[+] File %s \ | ||
| 59 | :\nNo harmful metadata found" % clean) | ||
| 60 | |||
| 61 | def test_list_dirty(self): | ||
| 62 | """check if get_meta returns all the expected meta""" | ||
| 63 | for _, dirty in self.file_list: | ||
| 64 | proc = subprocess.Popen(['mat', '-d', dirty], | ||
| 65 | stdout=subprocess.PIPE) | ||
| 66 | stdout, _ = proc.communicate() | ||
| 67 | self.assertNotEqual(str(stdout), "[+] File %s :\n No\ | ||
| 68 | harmul metadata found" % dirty) | ||
| 69 | |||
| 70 | |||
| 71 | class TestisCleancli(test.MATTest): | ||
| 72 | """ | ||
| 73 | check if cli correctly check if a file is clean or not | ||
| 74 | """ | ||
| 75 | |||
| 76 | def test_clean(self): | ||
| 77 | """test is_clean on clean files""" | ||
| 78 | for clean, _ in self.file_list: | ||
| 79 | proc = subprocess.Popen(['mat', '-c', clean], | ||
| 80 | stdout=subprocess.PIPE) | ||
| 81 | stdout, _ = proc.communicate() | ||
| 82 | self.assertEqual(str(stdout).strip('\n'), '[+] %s is clean' % clean) | ||
| 83 | |||
| 84 | def test_dirty(self): | ||
| 85 | """test is_clean on dirty files""" | ||
| 86 | for _, dirty in self.file_list: | ||
| 87 | proc = subprocess.Popen(['mat', '-c', dirty], | ||
| 88 | stdout=subprocess.PIPE) | ||
| 89 | stdout, _ = proc.communicate() | ||
| 90 | self.assertEqual(str(stdout).strip('\n'), '[+] %s is not clean' % dirty) | ||
| 91 | |||
| 92 | |||
| 93 | class TestFileAttributes(unittest.TestCase): | ||
| 94 | """ | ||
| 95 | test various stuffs about files (readable, writable, exist, ...) | ||
| 96 | """ | ||
| 97 | |||
| 98 | def test_not_writtable(self): | ||
| 99 | """ test MAT's behaviour on non-writable file""" | ||
| 100 | proc = subprocess.Popen(['mat', 'not_writtable'], | ||
| 101 | stdout=subprocess.PIPE) | ||
| 102 | stdout, _ = proc.communicate() | ||
| 103 | self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process not_writtable') | ||
| 104 | |||
| 105 | def test_not_exist(self): | ||
| 106 | """ test MAT's behaviour on non-existent file""" | ||
| 107 | proc = subprocess.Popen(['mat', 'ilikecookies'], | ||
| 108 | stdout=subprocess.PIPE) | ||
| 109 | stdout, _ = proc.communicate() | ||
| 110 | self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process ilikecookies') | ||
| 111 | |||
| 112 | def test_empty(self): | ||
| 113 | """ test MAT's behaviour on empty file""" | ||
| 114 | proc = subprocess.Popen(['mat', 'empty_file'], stdout=subprocess.PIPE) | ||
| 115 | stdout, _ = proc.communicate() | ||
| 116 | self.assertEqual(str(stdout).strip('\n'), '[-] Unable to process empty_file') | ||
| 117 | |||
| 118 | def test_not_readable(self): | ||
| 119 | """ test MAT's behaviour on non-writable file""" | ||
| 120 | open('non_readable', 'a').close() | ||
| 121 | os.chmod('non_readable', 0 & stat.S_IWRITE) | ||
| 122 | proc = subprocess.Popen(['mat', 'non_readable'], stdout=subprocess.PIPE) | ||
| 123 | stdout, _ = proc.communicate() | ||
| 124 | os.remove('non_readable') | ||
| 125 | |||
| 126 | |||
| 127 | class TestUnsupported(test.MATTest): | ||
| 128 | """ test MAT's behaviour on unsupported files """ | ||
| 129 | def test_abort_unsupported(self): | ||
| 130 | """ test if the cli aborts on unsupported files | ||
| 131 | """ | ||
| 132 | tarpath = os.path.join(self.tmpdir, "test.tar.bz2") | ||
| 133 | tar = tarfile.open(tarpath, "w") | ||
| 134 | for f in ('libtest.py', 'test.py', 'clitest.py'): | ||
| 135 | tar.add(f, f) | ||
| 136 | tar.close() | ||
| 137 | proc = subprocess.Popen(['mat', tarpath], stdout=subprocess.PIPE) | ||
| 138 | stdout, _ = proc.communicate() | ||
| 139 | self.assertTrue('It contains unsupported filetypes:' | ||
| 140 | '\n- libtest.py\n- test.py\n- clitest.py\n' | ||
| 141 | in str(stdout)) | ||
| 142 | |||
| 143 | |||
| 144 | class TestHelp(test.MATTest): | ||
| 145 | """ Test the different ways to trigger help """ | ||
| 146 | def test_dash_h(self): | ||
| 147 | """ test help invocation with `-h` and `--help` """ | ||
| 148 | proc = subprocess.Popen(['mat', '-h'], stdout=subprocess.PIPE) | ||
| 149 | stdout, _ = proc.communicate() | ||
| 150 | self.assertTrue('show this help message and exit' in stdout) | ||
| 151 | |||
| 152 | proc = subprocess.Popen(['mat', '--help'], stdout=subprocess.PIPE) | ||
| 153 | stdout, _ = proc.communicate() | ||
| 154 | self.assertTrue('show this help message and exit' in stdout) | ||
| 155 | |||
| 156 | def test_no_argument(self): | ||
| 157 | """ test help invocation when no argument is provided """ | ||
| 158 | proc = subprocess.Popen(['mat'], stdout=subprocess.PIPE) | ||
| 159 | stdout, _ = proc.communicate() | ||
| 160 | self.assertTrue('show this help message and exit' in stdout) | ||
| 161 | |||
| 162 | def test_wrong_argument(self): | ||
| 163 | """ Test MAT's behaviour on wrong argument """ | ||
| 164 | proc = subprocess.Popen(['mat', '--obviously-wrong-argument'], stderr=subprocess.PIPE) | ||
| 165 | _, stderr = proc.communicate() | ||
| 166 | self.assertTrue(('usage: mat [-h]' and ' error: unrecognized arguments:') in stderr) | ||
| 167 | |||
| 168 | |||
| 169 | def get_tests(): | ||
| 170 | """ Return every clitests""" | ||
| 171 | suite = unittest.TestSuite() | ||
| 172 | suite.addTest(unittest.makeSuite(TestRemovecli)) | ||
| 173 | suite.addTest(unittest.makeSuite(TestListcli)) | ||
| 174 | suite.addTest(unittest.makeSuite(TestisCleancli)) | ||
| 175 | suite.addTest(unittest.makeSuite(TestUnsupported)) | ||
| 176 | suite.addTest(unittest.makeSuite(TestFileAttributes)) | ||
| 177 | suite.addTest(unittest.makeSuite(TestHelp)) | ||
| 178 | return suite | ||
