2016-10-17 03:38:31 +00:00
|
|
|
import io
|
2015-06-18 08:36:58 +00:00
|
|
|
|
2016-12-01 09:36:18 +00:00
|
|
|
import pytest
|
|
|
|
|
2016-06-02 06:27:51 +00:00
|
|
|
from pathod import pathod
|
2016-10-19 22:56:38 +00:00
|
|
|
from mitmproxy.net import tcp
|
2016-10-19 22:27:05 +00:00
|
|
|
from mitmproxy import exceptions
|
2016-11-01 21:06:25 +00:00
|
|
|
from mitmproxy.test import tutils
|
2016-06-17 12:15:48 +00:00
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
from . import tservers
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2014-10-24 01:01:34 +00:00
|
|
|
|
2016-10-17 04:29:45 +00:00
|
|
|
class TestPathod:
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2012-06-21 02:29:49 +00:00
|
|
|
def test_logging(self):
|
2016-10-17 03:38:31 +00:00
|
|
|
s = io.StringIO()
|
2015-05-30 05:43:01 +00:00
|
|
|
p = pathod.Pathod(("127.0.0.1", 0), logfp=s)
|
2012-06-21 02:29:49 +00:00
|
|
|
assert len(p.get_log()) == 0
|
|
|
|
id = p.add_log(dict(s="foo"))
|
|
|
|
assert p.log_by_id(id)
|
|
|
|
assert len(p.get_log()) == 1
|
|
|
|
p.clear_log()
|
|
|
|
assert len(p.get_log()) == 0
|
|
|
|
|
2015-04-22 03:49:17 +00:00
|
|
|
for _ in range(p.LOGBUF + 1):
|
2012-06-21 02:29:49 +00:00
|
|
|
p.add_log(dict(s="foo"))
|
|
|
|
assert len(p.get_log()) <= p.LOGBUF
|
2012-06-26 05:28:07 +00:00
|
|
|
|
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class TestTimeout(tservers.DaemonTests):
|
2014-03-10 04:19:46 +00:00
|
|
|
timeout = 0.01
|
2014-10-25 02:30:54 +00:00
|
|
|
|
2016-06-05 01:24:46 +00:00
|
|
|
def test_timeout(self):
|
2012-09-30 23:01:02 +00:00
|
|
|
# FIXME: Add float values to spec language, reduce test timeout to
|
|
|
|
# increase test performance
|
2015-04-17 06:02:03 +00:00
|
|
|
# This is a bodge - we have some platform difference that causes
|
|
|
|
# different exceptions to be raised here.
|
2017-02-01 15:48:46 +00:00
|
|
|
with pytest.raises(Exception):
|
|
|
|
self.pathoc(["get:/:p1,1"])
|
2012-09-30 23:01:02 +00:00
|
|
|
assert self.d.last_log()["type"] == "timeout"
|
|
|
|
|
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class TestNotAfterConnect(tservers.DaemonTests):
|
2013-01-05 07:36:06 +00:00
|
|
|
ssl = False
|
2014-03-02 00:45:35 +00:00
|
|
|
ssloptions = dict(
|
2015-06-18 16:12:11 +00:00
|
|
|
not_after_connect=True
|
2014-03-02 00:45:35 +00:00
|
|
|
)
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2013-01-05 07:36:06 +00:00
|
|
|
def test_connect(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc(
|
2015-05-31 06:38:11 +00:00
|
|
|
[r"get:'http://foo.com/p/202':da"],
|
2014-10-24 01:01:34 +00:00
|
|
|
connect_to=("localhost", self.d.port)
|
|
|
|
)
|
2015-05-31 06:38:11 +00:00
|
|
|
assert r[0].status_code == 202
|
2013-01-05 07:36:06 +00:00
|
|
|
|
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class TestCustomCert(tservers.DaemonTests):
|
2014-03-02 02:13:56 +00:00
|
|
|
ssl = True
|
|
|
|
ssloptions = dict(
|
2017-10-24 19:12:39 +00:00
|
|
|
certs=[("*", tutils.test_data.path("pathod/data/testkey.pem"))],
|
2014-03-02 02:13:56 +00:00
|
|
|
)
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2014-03-02 02:13:56 +00:00
|
|
|
def test_connect(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc([r"get:/p/202"])
|
|
|
|
r = r[0]
|
2014-03-02 02:13:56 +00:00
|
|
|
assert r.status_code == 202
|
|
|
|
assert r.sslinfo
|
2015-04-18 22:12:15 +00:00
|
|
|
assert "test.com" in str(r.sslinfo.certchain[0].get_subject())
|
2014-03-02 02:13:56 +00:00
|
|
|
|
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class TestSSLCN(tservers.DaemonTests):
|
2014-03-02 00:45:35 +00:00
|
|
|
ssl = True
|
|
|
|
ssloptions = dict(
|
2016-06-15 08:15:35 +00:00
|
|
|
cn=b"foo.com"
|
2014-03-02 00:45:35 +00:00
|
|
|
)
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2014-03-02 00:45:35 +00:00
|
|
|
def test_connect(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc([r"get:/p/202"])
|
|
|
|
r = r[0]
|
2014-03-02 00:45:35 +00:00
|
|
|
assert r.status_code == 202
|
|
|
|
assert r.sslinfo
|
|
|
|
assert r.sslinfo.certchain[0].get_subject().CN == "foo.com"
|
|
|
|
|
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class TestNohang(tservers.DaemonTests):
|
2012-07-26 08:01:51 +00:00
|
|
|
nohang = True
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2012-07-26 08:01:51 +00:00
|
|
|
def test_nohang(self):
|
|
|
|
r = self.get("200:p0,0")
|
|
|
|
assert r.status_code == 800
|
2012-07-30 05:29:36 +00:00
|
|
|
l = self.d.last_log()
|
2014-03-02 09:10:49 +00:00
|
|
|
assert "Pauses have been disabled" in l["response"]["msg"]
|
2012-07-26 08:01:51 +00:00
|
|
|
|
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class TestHexdump(tservers.DaemonTests):
|
2012-10-24 21:59:18 +00:00
|
|
|
hexdump = True
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2012-10-24 21:59:18 +00:00
|
|
|
def test_hexdump(self):
|
2016-05-29 11:33:20 +00:00
|
|
|
assert self.get(r"200:b'\xf0'")
|
2012-10-24 21:59:18 +00:00
|
|
|
|
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class TestNocraft(tservers.DaemonTests):
|
2015-06-04 08:36:50 +00:00
|
|
|
nocraft = True
|
|
|
|
|
|
|
|
def test_nocraft(self):
|
|
|
|
r = self.get(r"200:b'\xf0'")
|
|
|
|
assert r.status_code == 800
|
2016-06-15 08:15:35 +00:00
|
|
|
assert b"Crafting disabled" in r.content
|
2015-06-04 08:36:50 +00:00
|
|
|
|
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class CommonTests(tservers.DaemonTests):
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2012-10-24 21:59:18 +00:00
|
|
|
def test_binarydata(self):
|
2016-05-29 11:33:20 +00:00
|
|
|
assert self.get(r"200:b'\xf0'")
|
|
|
|
assert self.d.last_log()
|
2012-10-24 21:59:18 +00:00
|
|
|
# FIXME: Other binary data elements
|
|
|
|
|
2012-07-23 03:03:56 +00:00
|
|
|
def test_sizelimit(self):
|
|
|
|
r = self.get("200:b@1g")
|
|
|
|
assert r.status_code == 800
|
2012-07-30 05:29:36 +00:00
|
|
|
l = self.d.last_log()
|
2014-03-02 09:10:49 +00:00
|
|
|
assert "too large" in l["response"]["msg"]
|
2012-07-23 03:03:56 +00:00
|
|
|
|
2012-07-21 02:14:31 +00:00
|
|
|
def test_preline(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc([r"get:'/p/200':i0,'\r\n'"])
|
|
|
|
assert r[0].status_code == 200
|
2012-07-21 02:14:31 +00:00
|
|
|
|
2012-06-26 05:28:07 +00:00
|
|
|
def test_logs(self):
|
2016-06-02 06:10:36 +00:00
|
|
|
self.d.clear_log()
|
2016-05-29 11:33:20 +00:00
|
|
|
assert self.get("202:da")
|
2016-06-02 06:10:36 +00:00
|
|
|
assert self.d.expect_log(1)
|
|
|
|
self.d.clear_log()
|
2012-06-26 05:28:07 +00:00
|
|
|
assert len(self.d.log()) == 0
|
|
|
|
|
|
|
|
def test_disconnect(self):
|
2017-02-06 16:48:44 +00:00
|
|
|
with pytest.raises(Exception, match="Unexpected EOF"):
|
2017-02-01 15:48:46 +00:00
|
|
|
self.get("202:b@100k:d200")
|
2012-06-26 05:28:07 +00:00
|
|
|
|
|
|
|
def test_parserr(self):
|
|
|
|
rsp = self.get("400:msg,b:")
|
|
|
|
assert rsp.status_code == 800
|
|
|
|
|
|
|
|
def test_static(self):
|
|
|
|
rsp = self.get("200:b<file")
|
|
|
|
assert rsp.status_code == 200
|
2016-06-15 08:15:35 +00:00
|
|
|
assert rsp.content.strip() == b"testfile"
|
2012-06-26 05:28:07 +00:00
|
|
|
|
|
|
|
def test_anchor(self):
|
2016-06-02 07:32:50 +00:00
|
|
|
rsp = self.getpath("/anchor/foo")
|
2012-06-26 05:28:07 +00:00
|
|
|
assert rsp.status_code == 202
|
2012-07-20 01:21:33 +00:00
|
|
|
|
|
|
|
def test_invalid_first_line(self):
|
2014-01-28 18:28:20 +00:00
|
|
|
c = tcp.TCPClient(("localhost", self.d.port))
|
2016-06-06 23:03:45 +00:00
|
|
|
with c.connect():
|
|
|
|
if self.ssl:
|
|
|
|
c.convert_to_ssl()
|
2016-06-15 08:15:35 +00:00
|
|
|
c.wfile.write(b"foo\n\n\n")
|
2016-06-06 23:03:45 +00:00
|
|
|
c.wfile.flush()
|
|
|
|
l = self.d.last_log()
|
|
|
|
assert l["type"] == "error"
|
|
|
|
assert "foo" in l["msg"]
|
2012-07-20 01:21:33 +00:00
|
|
|
|
2014-11-11 00:34:02 +00:00
|
|
|
def test_invalid_content_length(self):
|
2017-02-01 15:48:46 +00:00
|
|
|
with pytest.raises(exceptions.HttpException):
|
|
|
|
self.pathoc(["get:/:h'content-length'='foo'"])
|
2012-07-30 05:29:36 +00:00
|
|
|
l = self.d.last_log()
|
2012-07-22 00:30:10 +00:00
|
|
|
assert l["type"] == "error"
|
2015-09-16 16:44:34 +00:00
|
|
|
assert "Unparseable Content Length" in l["msg"]
|
2012-07-22 00:30:10 +00:00
|
|
|
|
2012-07-30 01:52:40 +00:00
|
|
|
def test_invalid_headers(self):
|
2017-02-01 15:48:46 +00:00
|
|
|
with pytest.raises(exceptions.HttpException):
|
|
|
|
self.pathoc(["get:/:h'\t'='foo'"])
|
2012-07-30 05:29:36 +00:00
|
|
|
l = self.d.last_log()
|
2012-07-30 01:52:40 +00:00
|
|
|
assert l["type"] == "error"
|
|
|
|
assert "Invalid headers" in l["msg"]
|
|
|
|
|
2012-07-22 11:37:46 +00:00
|
|
|
def test_access_denied(self):
|
|
|
|
rsp = self.get("=nonexistent")
|
|
|
|
assert rsp.status_code == 800
|
|
|
|
|
2012-07-23 05:53:17 +00:00
|
|
|
def test_source_access_denied(self):
|
|
|
|
rsp = self.get("200:b</foo")
|
|
|
|
assert rsp.status_code == 800
|
2016-06-15 08:15:35 +00:00
|
|
|
assert b"File access denied" in rsp.content
|
2012-07-23 05:53:17 +00:00
|
|
|
|
2013-01-05 04:06:41 +00:00
|
|
|
def test_proxy(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc([r"get:'http://foo.com/p/202':da"])
|
|
|
|
assert r[0].status_code == 202
|
2013-01-05 04:06:41 +00:00
|
|
|
|
2015-04-23 05:11:20 +00:00
|
|
|
def test_websocket(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc(["ws:/p/"], ws_read_limit=0)
|
|
|
|
assert r[0].status_code == 101
|
2015-04-23 05:11:20 +00:00
|
|
|
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc(["ws:/p/ws"], ws_read_limit=0)
|
|
|
|
assert r[0].status_code == 101
|
2015-04-23 19:35:17 +00:00
|
|
|
|
2015-05-31 06:38:11 +00:00
|
|
|
def test_websocket_frame(self):
|
2015-06-08 03:40:58 +00:00
|
|
|
r, _ = self.pathoc(
|
2015-06-08 03:57:29 +00:00
|
|
|
["ws:/p/", "wf:f'wf:b\"test\"':pa,1"],
|
2015-06-08 03:40:58 +00:00
|
|
|
ws_read_limit=1
|
|
|
|
)
|
2016-06-15 08:15:35 +00:00
|
|
|
assert r[1].payload == b"test"
|
2015-06-04 11:57:23 +00:00
|
|
|
|
2015-06-08 03:23:56 +00:00
|
|
|
def test_websocket_frame_reflect_error(self):
|
|
|
|
r, _ = self.pathoc(
|
|
|
|
["ws:/p/", "wf:-mask:knone:f'wf:b@10':i13,'a'"],
|
2015-06-18 16:12:11 +00:00
|
|
|
ws_read_limit=1,
|
|
|
|
timeout=1
|
2015-06-08 03:23:56 +00:00
|
|
|
)
|
2015-09-25 22:40:01 +00:00
|
|
|
# FIXME: Race Condition?
|
2015-06-08 03:23:56 +00:00
|
|
|
assert "Parse error" in self.d.text_log()
|
|
|
|
|
|
|
|
def test_websocket_frame_disconnect_error(self):
|
|
|
|
self.pathoc(["ws:/p/", "wf:b@10:d3"], ws_read_limit=0)
|
|
|
|
assert self.d.last_log()
|
|
|
|
|
2012-07-20 01:21:33 +00:00
|
|
|
|
2012-07-23 04:18:47 +00:00
|
|
|
class TestDaemon(CommonTests):
|
2012-07-23 07:55:33 +00:00
|
|
|
ssl = False
|
2015-05-30 00:03:13 +00:00
|
|
|
|
2013-01-05 07:29:46 +00:00
|
|
|
def test_connect(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc(
|
2015-05-31 06:38:11 +00:00
|
|
|
[r"get:'http://foo.com/p/202':da"],
|
2014-03-02 00:45:35 +00:00
|
|
|
connect_to=("localhost", self.d.port),
|
|
|
|
ssl=True
|
2015-06-08 03:23:56 +00:00
|
|
|
)
|
|
|
|
assert r[0].status_code == 202
|
2013-01-05 07:29:46 +00:00
|
|
|
|
|
|
|
def test_connect_err(self):
|
2017-02-01 15:48:46 +00:00
|
|
|
with pytest.raises(exceptions.HttpException):
|
|
|
|
self.pathoc([r"get:'http://foo.com/p/202':da"], connect_to=("localhost", self.d.port))
|
2012-07-20 01:21:33 +00:00
|
|
|
|
|
|
|
|
2012-07-23 04:18:47 +00:00
|
|
|
class TestDaemonSSL(CommonTests):
|
2012-07-23 07:55:33 +00:00
|
|
|
ssl = True
|
2015-04-18 22:43:16 +00:00
|
|
|
|
|
|
|
def test_ssl_conn_failure(self):
|
2014-01-28 18:28:20 +00:00
|
|
|
c = tcp.TCPClient(("localhost", self.d.port))
|
2012-07-20 01:21:33 +00:00
|
|
|
c.rbufsize = 0
|
|
|
|
c.wbufsize = 0
|
2016-06-06 23:03:45 +00:00
|
|
|
with c.connect():
|
2016-06-15 08:15:35 +00:00
|
|
|
c.wfile.write(b"\0\0\0\0")
|
2017-02-01 15:48:46 +00:00
|
|
|
with pytest.raises(exceptions.TlsException):
|
|
|
|
c.convert_to_ssl()
|
2016-06-06 23:03:45 +00:00
|
|
|
l = self.d.last_log()
|
|
|
|
assert l["type"] == "error"
|
|
|
|
assert "SSL" in l["msg"]
|
2012-07-20 01:21:33 +00:00
|
|
|
|
2014-03-02 09:10:49 +00:00
|
|
|
def test_ssl_cipher(self):
|
2015-06-08 03:23:56 +00:00
|
|
|
r, _ = self.pathoc([r"get:/p/202"])
|
|
|
|
assert r[0].status_code == 202
|
2014-03-02 09:10:49 +00:00
|
|
|
assert self.d.last_log()["cipher"][1] > 0
|
2015-06-16 11:52:41 +00:00
|
|
|
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class TestHTTP2(tservers.DaemonTests):
|
2015-06-16 11:52:41 +00:00
|
|
|
ssl = True
|
|
|
|
nohang = True
|
|
|
|
|
2016-12-01 09:36:18 +00:00
|
|
|
def test_http2(self):
|
|
|
|
r, _ = self.pathoc(["GET:/"], ssl=True, use_http2=True)
|
|
|
|
assert r[0].status_code == 800
|