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

'''
    Unit test for the library
'''

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
14
import tempfile
import test
import unittest
jvoisin's avatar
jvoisin committed
15

jvoisin's avatar
jvoisin committed
16
sys.path.append('..')
17
import MAT
18

jvoisin's avatar
jvoisin committed
19

jvoisin's avatar
jvoisin committed
20
class TestRemovelib(test.MATTest):
jvoisin's avatar
jvoisin committed
21
    ''' test the remove_all() method
jvoisin's avatar
jvoisin committed
22
    '''
23
24
    def test_remove(self):
        '''make sure that the lib remove all compromizing meta'''
jvoisin's avatar
jvoisin committed
25
        for _, dirty in self.file_list:
26
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True)
27
            current_file.remove_all()
28
29
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True)
            self.assertTrue(current_file.is_clean())
30
31
32

    def test_remove_empty(self):
        '''Test removal with clean files'''
jvoisin's avatar
jvoisin committed
33
        for clean, _ in self.file_list:
34
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True)
35
            current_file.remove_all()
36
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True)
37
38
39
            self.assertTrue(current_file.is_clean())


jvoisin's avatar
jvoisin committed
40
class TestListlib(test.MATTest):
jvoisin's avatar
jvoisin committed
41
    ''' test the get_meta() method
jvoisin's avatar
jvoisin committed
42
    '''
43
    def test_list(self):
44
        '''check if get_meta returns metadata'''
jvoisin's avatar
jvoisin committed
45
        for _, dirty in self.file_list:
46
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True)
jvoisin's avatar
jvoisin committed
47
            self.assertIsNotNone(current_file.get_meta())
48
49

    def testlist_list_empty(self):
50
        '''check that a listing of a clean file returns an empty dict'''
jvoisin's avatar
jvoisin committed
51
        for clean, _ in self.file_list:
52
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True)
jvoisin's avatar
jvoisin committed
53
            self.assertEqual(current_file.get_meta(), dict())
54
55


jvoisin's avatar
jvoisin committed
56
class TestisCleanlib(test.MATTest):
jvoisin's avatar
jvoisin committed
57
    ''' Test the is_clean() method
jvoisin's avatar
jvoisin committed
58
    '''
59
    def test_dirty(self):
60
        '''test is_clean on dirty files'''
jvoisin's avatar
jvoisin committed
61
        for _, dirty in self.file_list:
62
            current_file = MAT.mat.create_class_file(dirty, False, add2archive=True)
jvoisin's avatar
jvoisin committed
63
            self.assertFalse(current_file.is_clean())
64
65

    def test_clean(self):
66
        '''test is_clean on clean files'''
jvoisin's avatar
jvoisin committed
67
        for clean, _ in self.file_list:
68
            current_file = MAT.mat.create_class_file(clean, False, add2archive=True)
jvoisin's avatar
jvoisin committed
69
            self.assertTrue(current_file.is_clean())
70

jvoisin's avatar
jvoisin committed
71

jvoisin's avatar
jvoisin committed
72
73
74
75
76
class TestFileAttributes(unittest.TestCase):
    '''
        test various stuffs about files (readable, writable, exist, ...)
    '''
    def test_not_exist(self):
jvoisin's avatar
jvoisin committed
77
        ''' test MAT's behaviour on non-existent file'''
jvoisin's avatar
jvoisin committed
78
        self.assertFalse(MAT.mat.create_class_file('non_existent_file', False, add2archive=True))
79

jvoisin's avatar
jvoisin committed
80
81
    def test_empty(self):
        ''' test MAT's behaviour on empty file'''
jvoisin's avatar
jvoisin committed
82
        open('empty_file', 'a').close()
83
        self.assertFalse(MAT.mat.create_class_file('empty_file', False, add2archive=True))
jvoisin's avatar
jvoisin committed
84
        os.remove('empty_file')
85

jvoisin's avatar
jvoisin committed
86

87
88
89
90
91
92
93
94
95
96
97
98
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(MAT.mat.secure_remove(file_to_remove))

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

jvoisin's avatar
jvoisin committed
101

jvoisin's avatar
jvoisin committed
102
class TestArchiveProcessing(test.MATTest):
103
    ''' Test archives processing
jvoisin's avatar
jvoisin committed
104
105
    '''
    def test_remove_bz2(self):
jvoisin's avatar
jvoisin committed
106
107
        ''' Test MAT's ability to process .tar.bz2
        '''
jvoisin's avatar
jvoisin committed
108
109
        tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
        tar = tarfile.open(tarpath, "w:bz2")
jvoisin's avatar
jvoisin committed
110
        for clean, dirty in self.file_list:
jvoisin's avatar
jvoisin committed
111
            tar.add(dirty)
112
            tar.add(clean)
jvoisin's avatar
jvoisin committed
113
        tar.close()
jvoisin's avatar
jvoisin committed
114
        current_file = MAT.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
115
        current_file.remove_all()
jvoisin's avatar
jvoisin committed
116
        current_file = MAT.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
117
118
119
        self.assertTrue(current_file.is_clean())

    def test_remove_tar(self):
jvoisin's avatar
jvoisin committed
120
121
        ''' Test MAT on tar files
        '''
jvoisin's avatar
jvoisin committed
122
123
        tarpath = os.path.join(self.tmpdir, "test.tar")
        tar = tarfile.open(tarpath, "w")
jvoisin's avatar
jvoisin committed
124
        for clean, dirty in self.file_list:
jvoisin's avatar
jvoisin committed
125
            tar.add(dirty)
126
            tar.add(clean)
jvoisin's avatar
jvoisin committed
127
        tar.close()
jvoisin's avatar
jvoisin committed
128
        current_file = MAT.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
129
        current_file.remove_all()
jvoisin's avatar
jvoisin committed
130
        current_file = MAT.mat.create_class_file(tarpath, False, add2archive=False)
jvoisin's avatar
jvoisin committed
131
        self.assertTrue(current_file.is_clean())
jvoisin's avatar
jvoisin committed
132

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

147
    def test_get_unsupported(self):
jvoisin's avatar
jvoisin committed
148
149
        ''' Test the get_unsupported feature, used by the GUI
        '''
150
151
152
153
154
155
156
157
158
        tarpath = os.path.join(self.tmpdir, "test.tar.bz2")
        tar = tarfile.open(tarpath, "w")
        for f in ('../mat.desktop', '../README.security', '../setup.py'):
            tar.add(f, f[3:])  # trim '../'
        tar.close()
        current_file = MAT.mat.create_class_file(tarpath, False, add2archive=False)
        unsupported_files = set(current_file.is_clean(list_unsupported=True))
        self.assertEqual(unsupported_files, set(('mat.desktop', 'README.security', 'setup.py')))

159
160
161
162
163
164
165
    def test_archive_unwritable_content(self):
        path = os.path.join(self.tmpdir, './unwritable_content.zip')
        shutil.copy2('./unwritable_content.zip', self.tmpdir)
        current_file = MAT.mat.create_class_file(path, False, add2archive=False)
        current_file.remove_all()
        current_file = MAT.mat.create_class_file(path, False, add2archive=False)
        self.assertTrue(current_file.is_clean())
jvoisin's avatar
jvoisin committed
166

167
def get_tests():
168
    ''' Returns every libtests'''
jvoisin's avatar
jvoisin committed
169
170
171
172
173
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestRemovelib))
    suite.addTest(unittest.makeSuite(TestListlib))
    suite.addTest(unittest.makeSuite(TestisCleanlib))
    suite.addTest(unittest.makeSuite(TestFileAttributes))
174
    suite.addTest(unittest.makeSuite(TestSecureRemove))
jvoisin's avatar
jvoisin committed
175
    suite.addTest(unittest.makeSuite(TestArchiveProcessing))
jvoisin's avatar
jvoisin committed
176
    return suite