libtest.py 6.55 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 shutil
jvoisin's avatar
jvoisin committed
11
import tarfile
jvoisin's avatar
jvoisin committed
12
13
import tempfile
import unittest
jvoisin's avatar
jvoisin committed
14

15
16
17
18
19
20
import test
if test.IS_LOCAL is True:
    # Are we testing the _local_ version of MAT?
    sys.path.insert(0, '..')
# else it will be in the path

jvoisin's avatar
jvoisin committed
21
import libmat
22

jvoisin's avatar
jvoisin committed
23

jvoisin's avatar
jvoisin committed
24
class TestRemovelib(test.MATTest):
jvoisin's avatar
jvoisin committed
25
26
27
    """ test the remove_all() method
    """

28
    def test_remove(self):
jvoisin's avatar
jvoisin committed
29
        """make sure that the lib remove all compromizing meta"""
jvoisin's avatar
jvoisin committed
30
        for _, dirty in self.file_list:
jvoisin's avatar
jvoisin committed
31
            current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
32
            current_file.remove_all()
jvoisin's avatar
jvoisin committed
33
            current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
34
            self.assertTrue(current_file.is_clean())
35
36

    def test_remove_empty(self):
jvoisin's avatar
jvoisin committed
37
        """Test removal with clean files"""
jvoisin's avatar
jvoisin committed
38
        for clean, _ in self.file_list:
jvoisin's avatar
jvoisin committed
39
            current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
40
            current_file.remove_all()
jvoisin's avatar
jvoisin committed
41
            current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
42
43
44
            self.assertTrue(current_file.is_clean())


jvoisin's avatar
jvoisin committed
45
class TestListlib(test.MATTest):
jvoisin's avatar
jvoisin committed
46
47
48
    """ test the get_meta() method
    """

49
    def test_list(self):
jvoisin's avatar
jvoisin committed
50
        """check if get_meta returns metadata"""
jvoisin's avatar
jvoisin committed
51
        for _, dirty in self.file_list:
jvoisin's avatar
jvoisin committed
52
            current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
jvoisin's avatar
jvoisin committed
53
            self.assertIsNotNone(current_file.get_meta())
54
55

    def testlist_list_empty(self):
jvoisin's avatar
jvoisin committed
56
        """check that a listing of a clean file returns an empty dict"""
jvoisin's avatar
jvoisin committed
57
        for clean, _ in self.file_list:
jvoisin's avatar
jvoisin committed
58
            current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
jvoisin's avatar
jvoisin committed
59
            self.assertEqual(current_file.get_meta(), dict())
60
61


jvoisin's avatar
jvoisin committed
62
class TestisCleanlib(test.MATTest):
jvoisin's avatar
jvoisin committed
63
64
65
    """ Test the is_clean() method
    """

66
    def test_dirty(self):
jvoisin's avatar
jvoisin committed
67
        """test is_clean on dirty files"""
jvoisin's avatar
jvoisin committed
68
        for _, dirty in self.file_list:
jvoisin's avatar
jvoisin committed
69
            current_file = libmat.mat.create_class_file(dirty, False, add2archive=True)
jvoisin's avatar
jvoisin committed
70
            self.assertFalse(current_file.is_clean())
71
72

    def test_clean(self):
jvoisin's avatar
jvoisin committed
73
        """test is_clean on clean files"""
jvoisin's avatar
jvoisin committed
74
        for clean, _ in self.file_list:
jvoisin's avatar
jvoisin committed
75
            current_file = libmat.mat.create_class_file(clean, False, add2archive=True)
jvoisin's avatar
jvoisin committed
76
            self.assertTrue(current_file.is_clean())
77

jvoisin's avatar
jvoisin committed
78

jvoisin's avatar
jvoisin committed
79
class TestFileAttributes(unittest.TestCase):
jvoisin's avatar
jvoisin committed
80
    """
jvoisin's avatar
jvoisin committed
81
        test various stuffs about files (readable, writable, exist, ...)
jvoisin's avatar
jvoisin committed
82
83
    """

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

jvoisin's avatar
jvoisin committed
88
    def test_empty(self):
jvoisin's avatar
jvoisin committed
89
        """ test MAT's behaviour on empty file"""
jvoisin's avatar
jvoisin committed
90
        open('empty_file', 'a').close()
jvoisin's avatar
jvoisin committed
91
        self.assertFalse(libmat.mat.create_class_file('empty_file', False, add2archive=True))
jvoisin's avatar
jvoisin committed
92
        os.remove('empty_file')
93

jvoisin's avatar
jvoisin committed
94

95
class TestSecureRemove(unittest.TestCase):
jvoisin's avatar
jvoisin committed
96
97
98
    """ Test the secure_remove function
    """

99
    def test_remove_existing(self):
jvoisin's avatar
jvoisin committed
100
101
        """ test the secure removal of an existing file
        """
102
        _, file_to_remove = tempfile.mkstemp()
jvoisin's avatar
jvoisin committed
103
        self.assertTrue(libmat.mat.secure_remove(file_to_remove))
104
105

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

jvoisin's avatar
jvoisin committed
110

jvoisin's avatar
jvoisin committed
111
class TestArchiveProcessing(test.MATTest):
jvoisin's avatar
jvoisin committed
112
113
114
    """ Test archives processing
    """

jvoisin's avatar
jvoisin committed
115
    def test_remove_bz2(self):
jvoisin's avatar
jvoisin committed
116
117
        """ Test MAT's ability to process .tar.bz2
        """
jvoisin's avatar
jvoisin committed
118
119
        tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
        tar = tarfile.open(tarpath, "w:bz2")
jvoisin's avatar
jvoisin committed
120
        for clean, dirty in self.file_list:
jvoisin's avatar
jvoisin committed
121
            tar.add(dirty)
122
            tar.add(clean)
jvoisin's avatar
jvoisin committed
123
        tar.close()
jvoisin's avatar
jvoisin committed
124
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
125
        current_file.remove_all()
jvoisin's avatar
jvoisin committed
126
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
127
128
129
        self.assertTrue(current_file.is_clean())

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

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

157
    def test_get_unsupported(self):
jvoisin's avatar
jvoisin committed
158
159
        """ Test the get_unsupported feature, used by the GUI
        """
160
161
        tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
        tar = tarfile.open(tarpath, "w")
162
163
        for f in ('libtest.py', 'test.py', 'clitest.py'):
            tar.add(f, f)
164
        tar.close()
jvoisin's avatar
jvoisin committed
165
        current_file = libmat.mat.create_class_file(tarpath, False, add2archive=False)
166
        unsupported_files = set(current_file.is_clean(list_unsupported=True))
167
        self.assertEqual(unsupported_files, {'libtest.py', 'test.py', 'clitest.py'})
168

169
170
171
    def test_archive_unwritable_content(self):
        path = os.path.join(self.tmpdir, './unwritable_content.zip')
        shutil.copy2('./unwritable_content.zip', self.tmpdir)
jvoisin's avatar
jvoisin committed
172
        current_file = libmat.mat.create_class_file(path, False, add2archive=False)
173
        current_file.remove_all()
jvoisin's avatar
jvoisin committed
174
        current_file = libmat.mat.create_class_file(path, False, add2archive=False)
175
        self.assertTrue(current_file.is_clean())
jvoisin's avatar
jvoisin committed
176

jvoisin's avatar
jvoisin committed
177

178
def get_tests():
jvoisin's avatar
jvoisin committed
179
    """ Returns every libtests"""
jvoisin's avatar
jvoisin committed
180
181
182
183
184
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestRemovelib))
    suite.addTest(unittest.makeSuite(TestListlib))
    suite.addTest(unittest.makeSuite(TestisCleanlib))
    suite.addTest(unittest.makeSuite(TestFileAttributes))
185
    suite.addTest(unittest.makeSuite(TestSecureRemove))
jvoisin's avatar
jvoisin committed
186
    suite.addTest(unittest.makeSuite(TestArchiveProcessing))
jvoisin's avatar
jvoisin committed
187
    return suite