-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
test_cut.py
193 lines (162 loc) · 6.89 KB
/
test_cut.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
from unittest import mock
import pyperclip
import pytest
from mitmproxy import certs
from mitmproxy import exceptions
from mitmproxy.addons import cut
from mitmproxy.addons import view
from mitmproxy.test import taddons
from mitmproxy.test import tflow
def test_extract(tdata):
tf = tflow.tflow(resp=True)
tests = [
["request.method", "GET"],
["request.scheme", "http"],
["request.host", "address"],
["request.http_version", "HTTP/1.1"],
["request.port", "22"],
["request.path", "/path"],
["request.url", "http://address:22/path"],
["request.text", "content"],
["request.content", b"content"],
["request.raw_content", b"content"],
["request.timestamp_start", "946681200"],
["request.timestamp_end", "946681201"],
["request.header[header]", "qvalue"],
["response.status_code", "200"],
["response.reason", "OK"],
["response.text", "message"],
["response.content", b"message"],
["response.raw_content", b"message"],
["response.header[header-response]", "svalue"],
["response.timestamp_start", "946681202"],
["response.timestamp_end", "946681203"],
["client_conn.peername.port", "22"],
["client_conn.peername.host", "127.0.0.1"],
["client_conn.tls_version", "TLSv1.2"],
["client_conn.sni", "address"],
["client_conn.tls_established", "true"],
["server_conn.address.port", "22"],
["server_conn.address.host", "address"],
["server_conn.peername.host", "192.168.0.1"],
["server_conn.tls_version", "TLSv1.2"],
["server_conn.sni", "address"],
["server_conn.tls_established", "true"],
]
for spec, expected in tests:
ret = cut.extract(spec, tf)
assert spec and ret == expected
with open(tdata.path("mitmproxy/net/data/text_cert"), "rb") as f:
d = f.read()
c1 = certs.Cert.from_pem(d)
tf.server_conn.certificate_list = [c1]
assert "CERTIFICATE" in cut.extract("server_conn.certificate_list", tf)
def test_extract_websocket():
tf = tflow.twebsocketflow(messages=True)
extracted_request_content = cut.extract("request.content", tf)
extracted_response_content = cut.extract("response.content", tf)
assert b"hello binary" in extracted_request_content
assert b"hello text" in extracted_request_content
assert b"it's me" in extracted_request_content
assert b"hello binary" in extracted_response_content
assert b"hello text" in extracted_response_content
assert b"it's me" in extracted_response_content
def test_extract_str():
tf = tflow.tflow()
tf.request.raw_content = b"\xff"
assert cut.extract_str("request.raw_content", tf) == r"b'\xff'"
def test_headername():
with pytest.raises(exceptions.CommandError):
cut.headername("header[foo.")
def qr(f):
with open(f, "rb") as fp:
return fp.read()
async def test_cut_clip(caplog):
v = view.View()
c = cut.Cut()
with taddons.context() as tctx:
tctx.master.addons.add(v, c)
v.add([tflow.tflow(resp=True)])
with mock.patch("pyperclip.copy") as pc:
tctx.command(c.clip, "@all", "request.method")
assert pc.called
with mock.patch("pyperclip.copy") as pc:
tctx.command(c.clip, "@all", "request.content")
assert pc.called
with mock.patch("pyperclip.copy") as pc:
tctx.command(c.clip, "@all", "request.method,request.content")
assert pc.called
with mock.patch("pyperclip.copy") as pc:
log_message = (
"Pyperclip could not find a " "copy/paste mechanism for your system."
)
pc.side_effect = pyperclip.PyperclipException(log_message)
tctx.command(c.clip, "@all", "request.method")
assert log_message in caplog.text
def test_cut_save(tmpdir):
f = str(tmpdir.join("path"))
v = view.View()
c = cut.Cut()
with taddons.context() as tctx:
tctx.master.addons.add(v, c)
v.add([tflow.tflow(resp=True)])
tctx.command(c.save, "@all", "request.method", f)
assert qr(f) == b"GET"
tctx.command(c.save, "@all", "request.content", f)
assert qr(f) == b"content"
tctx.command(c.save, "@all", "request.content", "+" + f)
assert qr(f) == b"content\ncontent"
v.add([tflow.tflow(resp=True)])
tctx.command(c.save, "@all", "request.method", f)
assert qr(f).splitlines() == [b"GET", b"GET"]
tctx.command(c.save, "@all", "request.method,request.content", f)
assert qr(f).splitlines() == [b"GET,b'content'", b"GET,b'content'"]
@pytest.mark.parametrize(
"exception, log_message",
[
(PermissionError, "Permission denied"),
(IsADirectoryError, "Is a directory"),
(FileNotFoundError, "No such file or directory"),
],
)
async def test_cut_save_open(exception, log_message, tmpdir, caplog):
f = str(tmpdir.join("path"))
v = view.View()
c = cut.Cut()
with taddons.context() as tctx:
tctx.master.addons.add(v, c)
v.add([tflow.tflow(resp=True)])
with mock.patch("mitmproxy.addons.cut.open") as m:
m.side_effect = exception(log_message)
tctx.command(c.save, "@all", "request.method", f)
assert log_message in caplog.text
def test_cut():
c = cut.Cut()
with taddons.context():
tflows = [tflow.tflow(resp=True)]
assert c.cut(tflows, ["request.method"]) == [["GET"]]
assert c.cut(tflows, ["request.scheme"]) == [["http"]]
assert c.cut(tflows, ["request.host"]) == [["address"]]
assert c.cut(tflows, ["request.port"]) == [["22"]]
assert c.cut(tflows, ["request.path"]) == [["/path"]]
assert c.cut(tflows, ["request.url"]) == [["http://address:22/path"]]
assert c.cut(tflows, ["request.content"]) == [[b"content"]]
assert c.cut(tflows, ["request.header[header]"]) == [["qvalue"]]
assert c.cut(tflows, ["request.header[unknown]"]) == [[""]]
assert c.cut(tflows, ["response.status_code"]) == [["200"]]
assert c.cut(tflows, ["response.reason"]) == [["OK"]]
assert c.cut(tflows, ["response.content"]) == [[b"message"]]
assert c.cut(tflows, ["response.header[header-response]"]) == [["svalue"]]
assert c.cut(tflows, ["moo"]) == [[""]]
with pytest.raises(exceptions.CommandError):
assert c.cut(tflows, ["__dict__"]) == [[""]]
with taddons.context():
tflows = [tflow.tflow(resp=False)]
assert c.cut(tflows, ["response.reason"]) == [[""]]
assert c.cut(tflows, ["response.header[key]"]) == [[""]]
for f in (tflow.ttcpflow(), tflow.tudpflow()):
c = cut.Cut()
with taddons.context():
tflows = [f]
assert c.cut(tflows, ["request.method"]) == [[""]]
assert c.cut(tflows, ["response.status"]) == [[""]]