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
222
223
224
225
226
227
228
229
230
231
232
233
234
|
import json
from json import JSONDecodeError
from pathlib import Path
from unittest import mock
from typing import List
from unittest.mock import patch
from mitmproxy.test import tflow
from mitmproxy.test import tutils
from examples.complex.webscanner_helper.urlindex import UrlIndexWriter, SetEncoder, JSONUrlIndexWriter, TextUrlIndexWriter, WRITER, \
filter_404, \
UrlIndexAddon
class TestBaseClass:
@patch.multiple(UrlIndexWriter, __abstractmethods__=set())
def test_base_class(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
index_writer = UrlIndexWriter(tmpfile)
index_writer.load()
index_writer.add_url(tflow.tflow())
index_writer.save()
class TestSetEncoder:
def test_set_encoder_set(self):
test_set = {"foo", "bar", "42"}
result = SetEncoder.default(SetEncoder(), test_set)
assert isinstance(result, List)
assert 'foo' in result
assert 'bar' in result
assert '42' in result
def test_set_encoder_str(self):
test_str = "test"
try:
SetEncoder.default(SetEncoder(), test_str)
except TypeError:
assert True
else:
assert False
class TestJSONUrlIndexWriter:
def test_load(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
with open(tmpfile, "w") as tfile:
tfile.write(
"{\"http://example.com:80\": {\"/\": {\"GET\": [301]}}, \"http://www.example.com:80\": {\"/\": {\"GET\": [302]}}}")
writer = JSONUrlIndexWriter(filename=tmpfile)
writer.load()
assert 'http://example.com:80' in writer.host_urls
assert '/' in writer.host_urls['http://example.com:80']
assert 'GET' in writer.host_urls['http://example.com:80']['/']
assert 301 in writer.host_urls['http://example.com:80']['/']['GET']
def test_load_empty(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
with open(tmpfile, "w") as tfile:
tfile.write("{}")
writer = JSONUrlIndexWriter(filename=tmpfile)
writer.load()
assert len(writer.host_urls) == 0
def test_load_nonexisting(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
writer = JSONUrlIndexWriter(filename=tmpfile)
writer.load()
assert len(writer.host_urls) == 0
def test_add(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
writer = JSONUrlIndexWriter(filename=tmpfile)
f = tflow.tflow(resp=tutils.tresp())
url = f"{f.request.scheme}://{f.request.host}:{f.request.port}"
writer.add_url(f)
assert url in writer.host_urls
assert f.request.path in writer.host_urls[url]
def test_save(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
writer = JSONUrlIndexWriter(filename=tmpfile)
f = tflow.tflow(resp=tutils.tresp())
url = f"{f.request.scheme}://{f.request.host}:{f.request.port}"
writer.add_url(f)
writer.save()
with open(tmpfile, "r") as results:
try:
content = json.load(results)
except JSONDecodeError:
assert False
assert url in content
class TestTestUrlIndexWriter:
def test_load(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
with open(tmpfile, "w") as tfile:
tfile.write(
"2020-04-22T05:41:08.679231 STATUS: 200 METHOD: GET URL:http://example.com")
writer = TextUrlIndexWriter(filename=tmpfile)
writer.load()
assert True
def test_load_empty(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
with open(tmpfile, "w") as tfile:
tfile.write("{}")
writer = TextUrlIndexWriter(filename=tmpfile)
writer.load()
assert True
def test_load_nonexisting(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
writer = TextUrlIndexWriter(filename=tmpfile)
writer.load()
assert True
def test_add(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
writer = TextUrlIndexWriter(filename=tmpfile)
f = tflow.tflow(resp=tutils.tresp())
url = f"{f.request.scheme}://{f.request.host}:{f.request.port}"
method = f.request.method
code = f.response.status_code
writer.add_url(f)
with open(tmpfile, "r") as results:
content = results.read()
assert url in content
assert method in content
assert str(code) in content
def test_save(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
writer = TextUrlIndexWriter(filename=tmpfile)
f = tflow.tflow(resp=tutils.tresp())
url = f"{f.request.scheme}://{f.request.host}:{f.request.port}"
method = f.request.method
code = f.response.status_code
writer.add_url(f)
writer.save()
with open(tmpfile, "r") as results:
content = results.read()
assert url in content
assert method in content
assert str(code) in content
class TestWriter:
def test_writer_dict(self):
assert "json" in WRITER
assert isinstance(WRITER["json"], JSONUrlIndexWriter.__class__)
assert "text" in WRITER
assert isinstance(WRITER["text"], TextUrlIndexWriter.__class__)
class TestFilter:
def test_filer_true(self):
f = tflow.tflow(resp=tutils.tresp())
assert filter_404(f)
def test_filter_false(self):
f = tflow.tflow(resp=tutils.tresp())
f.response.status_code = 404
assert not filter_404(f)
class TestUrlIndexAddon:
def test_init(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
UrlIndexAddon(tmpfile)
def test_init_format(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
try:
UrlIndexAddon(tmpfile, index_format="test")
except ValueError:
assert True
else:
assert False
def test_init_filter(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
try:
UrlIndexAddon(tmpfile, index_filter="i~nvalid")
except ValueError:
assert True
else:
assert False
def test_init_append(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
with open(tmpfile, "w") as tfile:
tfile.write("")
url_index = UrlIndexAddon(tmpfile, append=False)
f = tflow.tflow(resp=tutils.tresp())
with mock.patch('examples.complex.webscanner_helper.urlindex.JSONUrlIndexWriter.add_url'):
url_index.response(f)
assert not Path(tmpfile).exists()
def test_response(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
url_index = UrlIndexAddon(tmpfile)
f = tflow.tflow(resp=tutils.tresp())
with mock.patch('examples.complex.webscanner_helper.urlindex.JSONUrlIndexWriter.add_url') as mock_add_url:
url_index.response(f)
mock_add_url.assert_called()
def test_response_None(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
url_index = UrlIndexAddon(tmpfile)
url_index.index_filter = None
f = tflow.tflow(resp=tutils.tresp())
try:
url_index.response(f)
except ValueError:
assert True
else:
assert False
def test_done(self, tmpdir):
tmpfile = tmpdir.join("tmpfile")
url_index = UrlIndexAddon(tmpfile)
with mock.patch('examples.complex.webscanner_helper.urlindex.JSONUrlIndexWriter.save') as mock_save:
url_index.done()
mock_save.assert_called()
|