-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.py
193 lines (169 loc) · 6.99 KB
/
test.py
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
# encoding: utf-8
from __future__ import print_function, unicode_literals
import datetime
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
from os.path import join as j
def is_non_zero_file(fpath):
return os.path.isfile(fpath) and os.path.getsize(fpath) > 0
class SelfCleaningTestCase(unittest.TestCase):
"""TestCase subclass which cleans up self.tmpdir after each test"""
def setUp(self):
super(SelfCleaningTestCase, self).setUp()
# tempdir for brunnhilde outputs
self.dest_tmpdir = tempfile.mkdtemp()
if not os.path.isdir(self.dest_tmpdir):
os.mkdirs(self.dest_tmpdir)
self.TEST_REPORT_DIR = os.path.join(self.dest_tmpdir, "test")
def tearDown(self):
if os.path.isdir(self.dest_tmpdir):
shutil.rmtree(self.dest_tmpdir)
super(SelfCleaningTestCase, self).tearDown()
class TestBrunnhildeIntegration(SelfCleaningTestCase):
"""
Integration tests. sf (Siegfried) must be installed on user's system for tests to work.
"""
def test_integration_existing_output_dir_quits(self):
os.makedirs(self.TEST_REPORT_DIR)
subprocess.call(
'python brunnhilde.py -n ./test-data/files/ "%s" test' % (self.dest_tmpdir),
shell=True,
)
self.assertFalse(is_non_zero_file(j(self.TEST_REPORT_DIR, "siegfried.csv")))
def test_integration_existing_output_dir_overwrites(self):
os.makedirs(self.TEST_REPORT_DIR)
subprocess.call(
'python brunnhilde.py -n --overwrite ./test-data/files/ "%s" test'
% (self.dest_tmpdir),
shell=True,
)
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "siegfried.csv")))
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "report.html")))
def test_integration_simple_positional_args(self):
"""Test `brunnhilde.py src dest` syntax introduced in 1.9.0.
"""
subprocess.call(
'python brunnhilde.py -n ./test-data/files/ "%s"' % (self.TEST_REPORT_DIR),
shell=True,
)
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "siegfried.csv")))
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "report.html")))
def test_integration_outputs_created(self):
subprocess.call(
'python brunnhilde.py -n ./test-data/files/ "%s" test' % (self.dest_tmpdir),
shell=True,
)
# siegfried csv and sqlite db
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "siegfried.csv")))
# html report
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "report.html")))
# csv reports
self.assertTrue(
is_non_zero_file(j(self.TEST_REPORT_DIR, "csv_reports", "formats.csv"))
)
self.assertTrue(
is_non_zero_file(
j(self.TEST_REPORT_DIR, "csv_reports", "formatVersions.csv")
)
)
self.assertTrue(
is_non_zero_file(j(self.TEST_REPORT_DIR, "csv_reports", "mimetypes.csv"))
)
self.assertTrue(
is_non_zero_file(j(self.TEST_REPORT_DIR, "csv_reports", "years.csv"))
)
# tree.txt
if not sys.platform.startswith("win"):
self.assertTrue(os.path.isfile(j(self.TEST_REPORT_DIR, "tree.txt")))
def test_integration_outputs_created_diskimage(self):
subprocess.call(
'python brunnhilde.py -nd ./test-data/diskimages/sample-floppy-fat.dd "%s" test'
% (self.dest_tmpdir),
shell=True,
)
# siegfried csv and sqlite db
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "siegfried.csv")))
# html report
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "report.html")))
# csv reports
self.assertTrue(
is_non_zero_file(j(self.TEST_REPORT_DIR, "csv_reports", "formats.csv"))
)
self.assertTrue(
is_non_zero_file(
j(self.TEST_REPORT_DIR, "csv_reports", "formatVersions.csv")
)
)
self.assertTrue(
is_non_zero_file(j(self.TEST_REPORT_DIR, "csv_reports", "mimetypes.csv"))
)
self.assertTrue(
is_non_zero_file(j(self.TEST_REPORT_DIR, "csv_reports", "years.csv"))
)
# tree.txt
if not sys.platform.startswith("win"):
self.assertTrue(os.path.isfile(j(self.TEST_REPORT_DIR, "tree.txt")))
# dfxml
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "dfxml.xml")))
# carved_files
self.assertTrue(
is_non_zero_file(j(self.TEST_REPORT_DIR, "carved_files", "file1.txt.txt"))
)
self.assertTrue(
is_non_zero_file(j(self.TEST_REPORT_DIR, "carved_files", "Tulips.jpg"))
)
def test_integration_temp_files_deleted(self):
subprocess.call(
'python brunnhilde.py -n ./test-data/files/ "%s" test' % (self.dest_tmpdir),
shell=True,
)
# uniqueyears.csv
self.assertFalse(
os.path.isfile(j(self.TEST_REPORT_DIR, "csv_reports", "uniqueyears.csv"))
)
def test_integration_clamav(self):
subprocess.call(
'python brunnhilde.py ./test-data/files/ "%s" test' % (self.dest_tmpdir),
shell=True,
)
# virus log correctly written
virus_log = j(self.TEST_REPORT_DIR, "logs", "viruscheck-log.txt")
with open(virus_log, "r") as f:
self.assertTrue("Scanned files: 4" in f.read())
with open(virus_log, "r") as f:
self.assertTrue("Infected files: 0" in f.read())
def test_integration_clamav_largefiles(self):
subprocess.call(
'python brunnhilde.py -l ./test-data/files/ "%s" test' % (self.dest_tmpdir),
shell=True,
)
# virus log correctly written
virus_log = j(self.TEST_REPORT_DIR, "logs", "viruscheck-log.txt")
with open(virus_log, "r") as f:
self.assertTrue("Scanned files: 4" in f.read())
with open(virus_log, "r") as f:
self.assertTrue("Infected files: 0" in f.read())
def test_integration_clamav_diskimage(self):
subprocess.call(
'python brunnhilde.py -d ./test-data/diskimages/sample-floppy-fat.dd "%s" test'
% (self.dest_tmpdir),
shell=True,
)
# virus log correctly written
virus_log = j(self.TEST_REPORT_DIR, "logs", "viruscheck-log.txt")
with open(virus_log, "r") as f:
self.assertTrue("Scanned files: 2" in f.read())
with open(virus_log, "r") as f:
self.assertTrue("Infected files: 0" in f.read())
def test_integration_retain_sqlite_db(self):
subprocess.call(
'python brunnhilde.py -k ./test-data/files/ "%s" test' % (self.dest_tmpdir),
shell=True,
)
self.assertTrue(is_non_zero_file(j(self.TEST_REPORT_DIR, "siegfried.sqlite")))
if __name__ == "__main__":
unittest.main()