It's Riseup's twentieth year, help keep us going! If you can afford it, please contribute to our winter fundraising drive.

libtest.py 7.47 KB
Newer Older
jvoisin's avatar
jvoisin committed
1 2
#!/usr/bin/env python
# -*- coding: utf-8 -*
3

jvoisin's avatar
jvoisin committed
4
"""
5
    Unit test for the library
jvoisin's avatar
jvoisin committed
6
"""
7

jvoisin's avatar
jvoisin committed
8
import os
jvoisin's avatar
jvoisin committed
9
import sys
10
import stat
11
import shutil
jvoisin's avatar
jvoisin committed
12
import tarfile
jvoisin's avatar
jvoisin committed
13 14
import tempfile
import unittest
jvoisin's avatar
jvoisin committed
15

16
import test
17
import libmat
18

19

20
class TestRemovelib(test.MATTest):
jvoisin's avatar
jvoisin committed
21 22 23
    """ test the remove_all() method
    """

24
    def test_remove(self):
jvoisin's avatar
jvoisin committed
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 33 34 35 36 37 38 39 40
    def test_remove_fileformat_specific_options(self):
        """ test metadata removal with fileformat-specific options """
        for _, dirty in self.file_list:  # can't be faster than that :/
            if dirty.endswith('pdf'):
                current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
                current_file.remove_all()
                current_file = libmat.mat.create_class_file(dirty, False, add2archive=True, low_pdf_quality=True)
                self.assertTrue(current_file.is_clean())

41
    def test_remove_empty(self):
jvoisin's avatar
jvoisin committed
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 48 49
            self.assertTrue(current_file.is_clean())


50
class TestListlib(test.MATTest):
jvoisin's avatar
jvoisin committed
51 52 53
    """ test the get_meta() method
    """

54
    def test_list(self):
jvoisin's avatar
jvoisin committed
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):
jvoisin's avatar
jvoisin committed
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)
jvoisin's avatar
jvoisin committed
64
            self.assertEqual(current_file.get_meta(), dict())
65 66


67
class TestisCleanlib(test.MATTest):
jvoisin's avatar
jvoisin committed
68 69 70
    """ Test the is_clean() method
    """

71
    def test_dirty(self):
jvoisin's avatar
jvoisin committed
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):
jvoisin's avatar
jvoisin committed
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):
jvoisin's avatar
jvoisin committed
85
    """
86
        test various stuffs about files (readable, writable, exist, ...)
jvoisin's avatar
jvoisin committed
87 88
    """

89
    def test_not_exist(self):
jvoisin's avatar
jvoisin committed
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):
jvoisin's avatar
jvoisin committed
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 100 101 102 103 104 105 106 107 108 109
    def test_not_writtable(self):
        """ test MAT's behaviour on non-writable file"""
        self.assertFalse(libmat.mat.create_class_file('not_writtable', False, add2archive=True))

    def test_not_readable(self):
        """ test MAT's behaviour on non-readable file"""
        open('non_readable', 'a').close()
        os.chmod('non_readable', 0 | stat.S_IWRITE)
        self.assertFalse(libmat.mat.create_class_file('non_readable', False, add2archive=True))
        os.remove('non_readable')

jvoisin's avatar
jvoisin committed
110

111
class TestSecureRemove(unittest.TestCase):
jvoisin's avatar
jvoisin committed
112 113 114
    """ Test the secure_remove function
    """

115
    def test_remove_existing(self):
jvoisin's avatar
jvoisin committed
116 117
        """ test the secure removal of an existing file
        """
118
        _, file_to_remove = tempfile.mkstemp()
119
        self.assertTrue(libmat.mat.secure_remove(file_to_remove))
120 121

    def test_remove_fail(self):
jvoisin's avatar
jvoisin committed
122 123
        """ test the secure removal of an non-removable file
        """
124
        self.assertRaises(libmat.exceptions.UnableToWriteFile, libmat.mat.secure_remove, '/NOTREMOVABLE')
125

jvoisin's avatar
jvoisin committed
126

jvoisin's avatar
jvoisin committed
127
class TestArchiveProcessing(test.MATTest):
jvoisin's avatar
jvoisin committed
128 129 130
    """ Test archives processing
    """

jvoisin's avatar
jvoisin committed
131
    def test_remove_bz2(self):
jvoisin's avatar
jvoisin committed
132 133
        """ Test MAT's ability to process .tar.bz2
        """
jvoisin's avatar
jvoisin committed
134 135
        tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
        tar = tarfile.open(tarpath, "w:bz2")
jvoisin's avatar
jvoisin committed
136
        for clean, dirty in self.file_list:
jvoisin's avatar
jvoisin committed
137
            tar.add(dirty)
138
            tar.add(clean)
jvoisin's avatar
jvoisin committed
139
        tar.close()
140
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
141
        current_file.remove_all()
142
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
143 144 145
        self.assertTrue(current_file.is_clean())

    def test_remove_tar(self):
jvoisin's avatar
jvoisin committed
146 147
        """ Test MAT on tar files
        """
jvoisin's avatar
jvoisin committed
148 149
        tarpath = os.path.join(self.tmpdir, "test.tar")
        tar = tarfile.open(tarpath, "w")
jvoisin's avatar
jvoisin committed
150
        for clean, dirty in self.file_list:
jvoisin's avatar
jvoisin committed
151
            tar.add(dirty)
152
            tar.add(clean)
jvoisin's avatar
jvoisin committed
153
        tar.close()
154
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
155
        current_file.remove_all()
156
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
157
        self.assertTrue(current_file.is_clean())
158

159
    def test_remove_gz(self):
jvoisin's avatar
jvoisin committed
160 161
        """ Test MAT on tar.gz files
        """
162 163
        tarpath = os.path.join(self.tmpdir, "test.tar.gz")
        tar = tarfile.open(tarpath, "w")
jvoisin's avatar
jvoisin committed
164
        for clean, dirty in self.file_list:
165 166 167
            tar.add(dirty)
            tar.add(clean)
        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 172
        self.assertTrue(current_file.is_clean())

173
    def test_get_unsupported(self):
jvoisin's avatar
jvoisin committed
174 175
        """ Test the get_unsupported feature, used by the GUI
        """
176 177
        tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
        tar = tarfile.open(tarpath, "w")
178 179
        for f in ('libtest.py', 'test.py', 'clitest.py'):
            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, {'libtest.py', 'test.py', 'clitest.py'})
184

185 186 187
    def test_archive_unwritable_content(self):
        path = os.path.join(self.tmpdir, './unwritable_content.zip')
        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())
jvoisin's avatar
jvoisin committed
192

jvoisin's avatar
jvoisin committed
193

194
def get_tests():
jvoisin's avatar
jvoisin committed
195
    """ Returns every libtests"""
196 197 198 199 200
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestRemovelib))
    suite.addTest(unittest.makeSuite(TestListlib))
    suite.addTest(unittest.makeSuite(TestisCleanlib))
    suite.addTest(unittest.makeSuite(TestFileAttributes))
201
    suite.addTest(unittest.makeSuite(TestSecureRemove))
jvoisin's avatar
jvoisin committed
202
    suite.addTest(unittest.makeSuite(TestArchiveProcessing))
203
    return suite