summaryrefslogtreecommitdiffstats
path: root/test.py
blob: e0fa968acc05f0c2ae7b12478f162be294a0d881 (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
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python

###############################################################################
#                                                                             #
# Peekaboo Extended Email Attachment Behavior Observation Owl                 #
#                                                                             #
# test.py                                                                     #
###############################################################################
#                                                                             #
# Copyright (C) 2016-2018  science + computing ag                             #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or (at       #
# your option) any later version.                                             #
#                                                                             #
# This program is distributed in the hope that it will be useful, but         #
# WITHOUT ANY WARRANTY; without even the implied warranty of                  #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           #
# General Public License for more details.                                    #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################


import sys
import os
import unittest


# Add Peekaboo to PYTHONPATH
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from peekaboo.sample import Sample
from peekaboo.ruleset import RuleResult, Result
from peekaboo.db import PeekabooDatabase
from peekaboo.config import _set_config


class PeekabooDummyConfig(object):
    def __init__(self):
        self.db_con = None
        self.job_hash_regex = r'/var/lib/amavis/tmp/([^/]+)/parts.*'
        self.sample_base_dir = '/tmp'

    def set_db_con(self, db_con):
        self.db_con = db_con

    def get_db_con(self):
        return self.db_con


class PeekabooDummyDB(object):
    def sample_info2db(self, sample):
        pass

    def sample_info_fetch(self, sha256):
        pass

    def fetch_rule_result(self, sha256):
        return RuleResult('fake_db',
                          result=Result.checked,
                          reason='Test Case',
                          further_analysis=True)

    def sample_info_update(self, sample):
        pass

    def known(self, sha256):
        return False

    def in_progress(self, sha256):
        return True

    def _clear_in_progress(self):
        pass


class TestDatabase(unittest.TestCase):
    """
    Unittests for Peekaboo's database module.

    @author: Sebastian Deiss
    """
    @classmethod
    def setUpClass(cls):
        cls.test_db = os.path.abspath('./test.db')
        cls.conf = PeekabooDummyConfig()
        db_con = PeekabooDatabase('sqlite:///' + cls.test_db)
        cls.conf.set_db_con(db_con)
        _set_config(cls.conf)
        cls.sample = Sample(os.path.realpath(__file__))
        result = RuleResult('Unittest',
                            Result.unknown,
                            'This is just a test case.',
                            further_analysis=True)
        cls.sample.add_rule_result(result)
        cls.sample.determine_result()

    def test_1_analysis2db(self):
        self.conf.db_con.analysis2db(self.sample)

    def test_2_sample_info_fetch(self):
        sample_info = self.conf.db_con.sample_info_fetch(self.sample)
        self.assertEqual(self.sample.sha256sum, sample_info.sha256sum)

    def test_3_sample_info_update(self):
        result = RuleResult('Unittest',
                            Result.checked,
                            'This is another test case.',
                            further_analysis=False)
        self.sample.add_rule_result(result)
        self.sample.determine_result()
        self.conf.db_con.sample_info_update(self.sample)
        rule_result = self.conf.db_con.fetch_rule_result(self.sample)
        self.assertEqual(rule_result.result, Result.checked)
        self.assertEqual(rule_result.reason, 'This is another test case.')

    def test_4_fetch_rule_result(self):
        rule_result = self.conf.db_con.fetch_rule_result(self.sample)
        # RuleResults from the DB have 'db' as rule name
        self.assertEqual(rule_result.rule, 'db')
        self.assertEqual(rule_result.result, Result.checked)
        self.assertEqual(rule_result.reason, 'This is another test case.')
        # We assert True since the DB rule result always sets further_analysis to True
        self.assertTrue(rule_result.further_analysis)

    def test_5_known(self):
        self.assertTrue(self.conf.db_con.known(self.sample))
        self.assertFalse(self.conf.db_con.in_progress(self.sample))

    @classmethod
    def tearDownClass(cls):
        os.unlink(cls.test_db)


class TestSample(unittest.TestCase):
    """
    Unittests for Samples.

    @author: Sebastian Deiss
    """
    @classmethod
    def setUpClass(cls):
        cls.test_db = os.path.abspath('./test.db')
        cls.conf = PeekabooDummyConfig()
        db_con = PeekabooDatabase('sqlite:///' + cls.test_db)
        cls.conf.set_db_con(db_con)
        _set_config(cls.conf)
        cls.sample = Sample(os.path.realpath(__file__))

    def test_attribute_dict(self):
        self.sample.set_attr('Unittest', 'Hello World!')
        self.assertTrue(self.sample.has_attr('Unittest'))
        self.assertEqual(self.sample.get_attr('Unittest'), 'Hello World!')
        self.sample.set_attr('Unittest', 'Test', override=True)
        self.assertEqual(self.sample.get_attr('Unittest'), 'Test')

    def test_job_hash_regex(self):
        path_with_job_hash = '/var/lib/amavis/tmp/amavis-20170831T132736-07759-iSI0rJ4b/parts'
        sample = Sample(path_with_job_hash)
        job_hash = sample.get_job_hash()
        self.assertEqual(job_hash, 'amavis-20170831T132736-07759-iSI0rJ4b',
                         'Job hash regex is not working')
        job_hash = self.sample.get_job_hash()
        self.assertIn('peekaboo-run_analysis', job_hash)

    def test_sample_attributes(self):
        self.assertEqual(self.sample.get_filename(), 'test.py')
        self.assertEqual(self.sample.file_extension, 'py')
        self.assertTrue(set(['text/x-python']).issubset(set(self.sample.mimetypes)))
        self.assertIsNotNone(self.sample.sha256sum)
        self.assertEqual(self.sample.job_id, -1)
        self.assertEqual(self.sample.get_result(), Result.unchecked)
        self.assertEqual(self.sample.reason,
                         'Ausschlaggebendes Ergebnis laut Datenbank: Datei ist dem System noch nicht bekannt')
        self.assertFalse(self.sample.office_macros)
        self.assertFalse(self.sample.known)

    def test_sample_attributes_with_meta_info(self):
        sample = Sample('test.pyc', {
            'full_name': '/tmp/test.pyc',
            'name_declared': 'test.pyc',
            'type_declared': 'application/x-bytecode.python',
            'type_long': 'application/x-python-bytecode',
            'type_short': 'pyc',
            'size': '200' })
        self.assertEqual(sample.file_extension, 'pyc')

    def test_sample_without_suffix(self):
        sample = Sample('junk', {
            'full_name': '/tmp/junk',
            'name_declared': 'Report.docx',
            'type_declared': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type_long': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type_short': 'docx',
            'size': '212' })
        self.assertEqual(sample.file_extension, 'docx')

    @classmethod
    def tearDownClass(cls):
        os.unlink(cls.test_db)


def main():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestSample))
    suite.addTest(unittest.makeSuite(TestDatabase))
    # TODO: We need more tests!!!

    runner = unittest.TextTestRunner(verbosity=2)
    result = runner.run(suite)

    if not result.wasSuccessful():
        sys.exit(1)


if __name__ == '__main__':
    main()