summaryrefslogtreecommitdiff
path: root/test/test_lib.py
blob: 8d3d76667cf75cf524e42a99075387de96adaa56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
# -*- coding: utf-8 -*

"""
    Unit test for the library
"""

import os
import sys
import stat
import shutil
import tarfile
import tempfile
import unittest

import test
import libmat


class TestRemovelib(test.MATTest):
    """ test the remove_all() method
    """

    def test_remove(self):
        """make sure that the lib remove all compromizing meta"""
        for _, dirty in self.file_list:
            current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
            current_file.remove_all()
            current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
            self.assertTrue(current_file.is_clean())

    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())

    def test_remove_empty(self):
        """Test removal with clean files"""
        for clean, _ in self.file_list:
            current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
            current_file.remove_all()
            current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
            self.assertTrue(current_file.is_clean())


class TestListlib(test.MATTest):
    """ test the get_meta() method
    """

    def test_list(self):
        """check if get_meta returns metadata"""
        for _, dirty in self.file_list:
            current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
            self.assertIsNotNone(current_file.get_meta())

    def testlist_list_empty(self):
        """check that a listing of a clean file returns an empty dict"""
        for clean, _ in self.file_list:
            current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
            self.assertEqual(current_file.get_meta(), dict())


class TestisCleanlib(test.MATTest):
    """ Test the is_clean() method
    """

    def test_dirty(self):
        """test is_clean on dirty files"""
        for _, dirty in self.file_list:
            current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
            print(current_file.filename)
            self.assertFalse(current_file.is_clean())

    def test_clean(self):
        """test is_clean on clean files"""
        for clean, _ in self.file_list:
            current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
            self.assertTrue(current_file.is_clean())


class TestFileAttributes(unittest.TestCase):
    """
        test various stuffs about files (readable, writable, exist, ...)
    """

    def test_not_exist(self):
        """ test MAT's behaviour on non-existent file"""
        self.assertFalse(libmat.mat.create_class_file('non_existent_file', False, add2archive=True))

    def test_empty(self):
        """ test MAT's behaviour on empty file"""
        open('empty_file', 'a').close()
        self.assertFalse(libmat.mat.create_class_file('empty_file', False, add2archive=True))
        os.remove('empty_file')

    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')


class TestSecureRemove(unittest.TestCase):
    """ Test the secure_remove function
    """

    def test_remove_existing(self):
        """ test the secure removal of an existing file
        """
        _, file_to_remove = tempfile.mkstemp()
        self.assertTrue(libmat.mat.secure_remove(file_to_remove))

    def test_remove_fail(self):
        """ test the secure removal of an non-removable file
        """
        self.assertRaises(libmat.exceptions.UnableToWriteFile, libmat.mat.secure_remove, '/NOTREMOVABLE')


class TestArchiveProcessing(test.MATTest):
    """ Test archives processing
    """

    def test_remove_bz2(self):
        """ Test MAT's ability to process .tar.bz2
        """
        return
        tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
        tar = tarfile.open(tarpath, "w:bz2")
        for clean, dirty in self.file_list:
            tar.add(dirty)
            tar.add(clean)
        tar.close()
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
        current_file.remove_all()
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
        self.assertTrue(current_file.is_clean())

    def test_remove_tar(self):
        """ Test MAT on tar files
        """
        return
        tarpath = os.path.join(self.tmpdir, "test.tar")
        tar = tarfile.open(tarpath, "w")
        for clean, dirty in self.file_list:
            tar.add(dirty)
            tar.add(clean)
        tar.close()
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
        current_file.remove_all()
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
        self.assertTrue(current_file.is_clean())

    def test_remove_gz(self):
        """ Test MAT on tar.gz files
        """
        return
        tarpath = os.path.join(self.tmpdir, "test.tar.gz")
        tar = tarfile.open(tarpath, "w")
        for clean, dirty in self.file_list:
            tar.add(dirty)
            tar.add(clean)
        tar.close()
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
        current_file.remove_all()
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
        self.assertTrue(current_file.is_clean())

    def test_get_unsupported(self):
        """ Test the get_unsupported feature, used by the GUI
        """
        return
        tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
        tar = tarfile.open(tarpath, "w")
        for f in ('test_lib.py', 'test.py', 'test_cli.py'):
            tar.add(f, f)
        tar.close()
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
        unsupported_files = set(current_file.is_clean(list_unsupported=True))
        self.assertEqual(unsupported_files, {'test_lib.py', 'test.py', 'test_cli.py'})

    def test_archive_unwritable_content(self):
        return
        path = os.path.join(self.tmpdir, './unwritable_content.zip')
        shutil.copy2('./unwritable_content.zip', self.tmpdir)
        current_file = libmat.mat.create_class_file(path, False, add2archive=False)
        current_file.remove_all()
        current_file = libmat.mat.create_class_file(path, False, add2archive=False)
        self.assertTrue(current_file.is_clean())


def get_tests():
    """ Returns every libtests"""
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestRemovelib))
    suite.addTest(unittest.makeSuite(TestListlib))
    suite.addTest(unittest.makeSuite(TestisCleanlib))
    suite.addTest(unittest.makeSuite(TestFileAttributes))
    suite.addTest(unittest.makeSuite(TestSecureRemove))
    #suite.addTest(unittest.makeSuite(TestArchiveProcessing))
    return suite