diff options
Diffstat (limited to 'test/test_lib.py')
| -rw-r--r-- | test/test_lib.py | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/test/test_lib.py b/test/test_lib.py new file mode 100644 index 0000000..33b3a02 --- /dev/null +++ b/test/test_lib.py | |||
| @@ -0,0 +1,203 @@ | |||
| 1 | #!/usr/bin/env python | ||
| 2 | # -*- coding: utf-8 -* | ||
| 3 | |||
| 4 | """ | ||
| 5 | Unit test for the library | ||
| 6 | """ | ||
| 7 | |||
| 8 | import os | ||
| 9 | import sys | ||
| 10 | import stat | ||
| 11 | import shutil | ||
| 12 | import tarfile | ||
| 13 | import tempfile | ||
| 14 | import unittest | ||
| 15 | |||
| 16 | import test | ||
| 17 | import libmat | ||
| 18 | |||
| 19 | |||
| 20 | class TestRemovelib(test.MATTest): | ||
| 21 | """ test the remove_all() method | ||
| 22 | """ | ||
| 23 | |||
| 24 | def test_remove(self): | ||
| 25 | """make sure that the lib remove all compromizing meta""" | ||
| 26 | for _, dirty in self.file_list: | ||
| 27 | current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) | ||
| 28 | current_file.remove_all() | ||
| 29 | current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) | ||
| 30 | self.assertTrue(current_file.is_clean()) | ||
| 31 | |||
| 32 | def test_remove_fileformat_specific_options(self): | ||
| 33 | """ test metadata removal with fileformat-specific options """ | ||
| 34 | for _, dirty in self.file_list: # can't be faster than that :/ | ||
| 35 | if dirty.endswith('pdf'): | ||
| 36 | current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) | ||
| 37 | current_file.remove_all() | ||
| 38 | current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True) | ||
| 39 | self.assertTrue(current_file.is_clean()) | ||
| 40 | |||
| 41 | def test_remove_empty(self): | ||
| 42 | """Test removal with clean files""" | ||
| 43 | for clean, _ in self.file_list: | ||
| 44 | current_file = libmat.mat.create_class_file(clean, False, add2archive=True) | ||
| 45 | current_file.remove_all() | ||
| 46 | current_file = libmat.mat.create_class_file(clean, False, add2archive=True) | ||
| 47 | self.assertTrue(current_file.is_clean()) | ||
| 48 | |||
| 49 | |||
| 50 | class TestListlib(test.MATTest): | ||
| 51 | """ test the get_meta() method | ||
| 52 | """ | ||
| 53 | |||
| 54 | def test_list(self): | ||
| 55 | """check if get_meta returns metadata""" | ||
| 56 | for _, dirty in self.file_list: | ||
| 57 | current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) | ||
| 58 | self.assertIsNotNone(current_file.get_meta()) | ||
| 59 | |||
| 60 | def testlist_list_empty(self): | ||
| 61 | """check that a listing of a clean file returns an empty dict""" | ||
| 62 | for clean, _ in self.file_list: | ||
| 63 | current_file = libmat.mat.create_class_file(clean, False, add2archive=True) | ||
| 64 | self.assertEqual(current_file.get_meta(), dict()) | ||
| 65 | |||
| 66 | |||
| 67 | class TestisCleanlib(test.MATTest): | ||
| 68 | """ Test the is_clean() method | ||
| 69 | """ | ||
| 70 | |||
| 71 | def test_dirty(self): | ||
| 72 | """test is_clean on dirty files""" | ||
| 73 | for _, dirty in self.file_list: | ||
| 74 | current_file = libmat.mat.create_class_file(dirty, False, add2archive=True) | ||
| 75 | self.assertFalse(current_file.is_clean()) | ||
| 76 | |||
| 77 | def test_clean(self): | ||
| 78 | """test is_clean on clean files""" | ||
| 79 | for clean, _ in self.file_list: | ||
| 80 | current_file = libmat.mat.create_class_file(clean, False, add2archive=True) | ||
| 81 | self.assertTrue(current_file.is_clean()) | ||
| 82 | |||
| 83 | |||
| 84 | class TestFileAttributes(unittest.TestCase): | ||
| 85 | """ | ||
| 86 | test various stuffs about files (readable, writable, exist, ...) | ||
| 87 | """ | ||
| 88 | |||
| 89 | def test_not_exist(self): | ||
| 90 | """ test MAT's behaviour on non-existent file""" | ||
| 91 | self.assertFalse(libmat.mat.create_class_file('non_existent_file', False, add2archive=True)) | ||
| 92 | |||
| 93 | def test_empty(self): | ||
| 94 | """ test MAT's behaviour on empty file""" | ||
| 95 | open('empty_file', 'a').close() | ||
| 96 | self.assertFalse(libmat.mat.create_class_file('empty_file', False, add2archive=True)) | ||
| 97 | os.remove('empty_file') | ||
| 98 | |||
| 99 | def test_not_writtable(self): | ||
| 100 | """ test MAT's behaviour on non-writable file""" | ||
| 101 | self.assertFalse(libmat.mat.create_class_file('not_writtable', False, add2archive=True)) | ||
| 102 | |||
| 103 | def test_not_readable(self): | ||
| 104 | """ test MAT's behaviour on non-readable file""" | ||
| 105 | open('non_readable', 'a').close() | ||
| 106 | os.chmod('non_readable', 0 | stat.S_IWRITE) | ||
| 107 | self.assertFalse(libmat.mat.create_class_file('non_readable', False, add2archive=True)) | ||
| 108 | os.remove('non_readable') | ||
| 109 | |||
| 110 | |||
| 111 | class TestSecureRemove(unittest.TestCase): | ||
| 112 | """ Test the secure_remove function | ||
| 113 | """ | ||
| 114 | |||
| 115 | def test_remove_existing(self): | ||
| 116 | """ test the secure removal of an existing file | ||
| 117 | """ | ||
| 118 | _, file_to_remove = tempfile.mkstemp() | ||
| 119 | self.assertTrue(libmat.mat.secure_remove(file_to_remove)) | ||
| 120 | |||
| 121 | def test_remove_fail(self): | ||
| 122 | """ test the secure removal of an non-removable file | ||
| 123 | """ | ||
| 124 | self.assertRaises(libmat.exceptions.UnableToWriteFile, libmat.mat.secure_remove, '/NOTREMOVABLE') | ||
| 125 | |||
| 126 | |||
| 127 | class TestArchiveProcessing(test.MATTest): | ||
| 128 | """ Test archives processing | ||
| 129 | """ | ||
| 130 | |||
| 131 | def test_remove_bz2(self): | ||
| 132 | """ Test MAT's ability to process .tar.bz2 | ||
| 133 | """ | ||
| 134 | tarpath = os.path.join(self.tmpdir, "test.tar.bz2") | ||
| 135 | tar = tarfile.open(tarpath, "w:bz2") | ||
| 136 | for clean, dirty in self.file_list: | ||
| 137 | tar.add(dirty) | ||
| 138 | tar.add(clean) | ||
| 139 | tar.close() | ||
| 140 | current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) | ||
| 141 | current_file.remove_all() | ||
| 142 | current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) | ||
| 143 | self.assertTrue(current_file.is_clean()) | ||
| 144 | |||
| 145 | def test_remove_tar(self): | ||
| 146 | """ Test MAT on tar files | ||
| 147 | """ | ||
| 148 | tarpath = os.path.join(self.tmpdir, "test.tar") | ||
| 149 | tar = tarfile.open(tarpath, "w") | ||
| 150 | for clean, dirty in self.file_list: | ||
| 151 | tar.add(dirty) | ||
| 152 | tar.add(clean) | ||
| 153 | tar.close() | ||
| 154 | current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) | ||
| 155 | current_file.remove_all() | ||
| 156 | current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) | ||
| 157 | self.assertTrue(current_file.is_clean()) | ||
| 158 | |||
| 159 | def test_remove_gz(self): | ||
| 160 | """ Test MAT on tar.gz files | ||
| 161 | """ | ||
| 162 | tarpath = os.path.join(self.tmpdir, "test.tar.gz") | ||
| 163 | tar = tarfile.open(tarpath, "w") | ||
| 164 | for clean, dirty in self.file_list: | ||
| 165 | tar.add(dirty) | ||
| 166 | tar.add(clean) | ||
| 167 | tar.close() | ||
| 168 | current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) | ||
| 169 | current_file.remove_all() | ||
| 170 | current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) | ||
| 171 | self.assertTrue(current_file.is_clean()) | ||
| 172 | |||
| 173 | def test_get_unsupported(self): | ||
| 174 | """ Test the get_unsupported feature, used by the GUI | ||
| 175 | """ | ||
| 176 | tarpath = os.path.join(self.tmpdir, "test.tar.bz2") | ||
| 177 | tar = tarfile.open(tarpath, "w") | ||
| 178 | for f in ('test_lib.py', 'test.py', 'test_cli.py'): | ||
| 179 | tar.add(f, f) | ||
| 180 | tar.close() | ||
| 181 | current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False) | ||
| 182 | unsupported_files = set(current_file.is_clean(list_unsupported=True)) | ||
| 183 | self.assertEqual(unsupported_files, {'test_lib.py', 'test.py', 'test_cli.py'}) | ||
| 184 | |||
| 185 | def test_archive_unwritable_content(self): | ||
| 186 | path = os.path.join(self.tmpdir, './unwritable_content.zip') | ||
| 187 | shutil.copy2('./unwritable_content.zip', self.tmpdir) | ||
| 188 | current_file = libmat.mat.create_class_file(path, False, add2archive=False) | ||
| 189 | current_file.remove_all() | ||
| 190 | current_file = libmat.mat.create_class_file(path, False, add2archive=False) | ||
| 191 | self.assertTrue(current_file.is_clean()) | ||
| 192 | |||
| 193 | |||
| 194 | def get_tests(): | ||
| 195 | """ Returns every libtests""" | ||
| 196 | suite = unittest.TestSuite() | ||
| 197 | suite.addTest(unittest.makeSuite(TestRemovelib)) | ||
| 198 | suite.addTest(unittest.makeSuite(TestListlib)) | ||
| 199 | suite.addTest(unittest.makeSuite(TestisCleanlib)) | ||
| 200 | suite.addTest(unittest.makeSuite(TestFileAttributes)) | ||
| 201 | suite.addTest(unittest.makeSuite(TestSecureRemove)) | ||
| 202 | suite.addTest(unittest.makeSuite(TestArchiveProcessing)) | ||
| 203 | return suite | ||
