2016-10-17 03:38:31 +00:00
|
|
|
import io
|
2017-01-22 12:44:53 +00:00
|
|
|
from unittest.mock import Mock
|
2020-07-16 13:00:41 +00:00
|
|
|
|
2016-12-01 09:36:18 +00:00
|
|
|
import pytest
|
2014-10-25 02:30:54 +00:00
|
|
|
|
2016-10-19 22:27:05 +00:00
|
|
|
from mitmproxy import exceptions
|
2020-07-16 13:00:41 +00:00
|
|
|
from mitmproxy.net.http import http1
|
2016-11-01 21:06:25 +00:00
|
|
|
from mitmproxy.test import tutils
|
2020-07-16 13:00:41 +00:00
|
|
|
from pathod import language, pathoc
|
|
|
|
from pathod.protocols.http2 import HTTP2StateProtocol
|
2016-11-01 21:06:25 +00:00
|
|
|
from . import tservers
|
2012-06-26 05:28:07 +00:00
|
|
|
|
2014-09-06 23:38:44 +00:00
|
|
|
|
2016-11-01 21:06:25 +00:00
|
|
|
class PathocTestDaemon(tservers.DaemonTests):
|
2016-06-06 04:05:44 +00:00
|
|
|
def tval(self, requests, timeout=None, showssl=False, **kwargs):
|
2016-10-17 03:38:31 +00:00
|
|
|
s = io.StringIO()
|
2015-04-19 06:04:27 +00:00
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("127.0.0.1", self.d.port),
|
2015-06-18 16:12:11 +00:00
|
|
|
ssl=self.ssl,
|
2016-06-06 04:05:44 +00:00
|
|
|
fp=s,
|
|
|
|
**kwargs
|
2015-04-19 06:04:27 +00:00
|
|
|
)
|
2016-06-05 01:04:13 +00:00
|
|
|
with c.connect(showssl=showssl, fp=s):
|
|
|
|
if timeout:
|
|
|
|
c.settimeout(timeout)
|
|
|
|
for i in requests:
|
2016-06-15 05:03:56 +00:00
|
|
|
r = next(language.parse_pathoc(i))
|
2016-06-06 04:05:44 +00:00
|
|
|
if kwargs.get("explain"):
|
2016-06-05 01:04:13 +00:00
|
|
|
r = r.freeze(language.Settings())
|
|
|
|
try:
|
|
|
|
c.request(r)
|
2016-10-19 22:27:05 +00:00
|
|
|
except exceptions.NetlibException:
|
2016-06-05 01:04:13 +00:00
|
|
|
pass
|
2016-06-06 04:05:44 +00:00
|
|
|
self.d.wait_for_silence()
|
2014-03-02 06:04:56 +00:00
|
|
|
return s.getvalue()
|
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
|
2016-06-05 01:04:13 +00:00
|
|
|
class TestDaemonSSL(PathocTestDaemon):
|
2013-01-03 21:37:26 +00:00
|
|
|
ssl = True
|
2016-06-05 01:04:13 +00:00
|
|
|
ssloptions = dict(
|
2015-06-18 16:12:11 +00:00
|
|
|
request_client_cert=True,
|
2016-06-15 05:03:56 +00:00
|
|
|
sans=[b"test1.com", b"test2.com"],
|
2015-09-26 15:40:22 +00:00
|
|
|
alpn_select=b'h2',
|
2015-04-18 22:43:16 +00:00
|
|
|
)
|
2014-09-06 23:38:44 +00:00
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
def test_sni(self):
|
2016-06-06 04:05:44 +00:00
|
|
|
self.tval(
|
|
|
|
["get:/p/200"],
|
2016-07-07 04:03:17 +00:00
|
|
|
sni="foobar.com"
|
2013-01-03 21:37:26 +00:00
|
|
|
)
|
2016-06-05 01:04:13 +00:00
|
|
|
log = self.d.log()
|
2016-07-07 04:03:17 +00:00
|
|
|
assert log[0]["request"]["sni"] == "foobar.com"
|
2013-01-03 21:37:26 +00:00
|
|
|
|
2014-03-02 06:04:56 +00:00
|
|
|
def test_showssl(self):
|
2014-09-06 23:38:44 +00:00
|
|
|
assert "certificate chain" in self.tval(["get:/p/200"], showssl=True)
|
2014-03-02 06:04:56 +00:00
|
|
|
|
2018-04-22 23:05:58 +00:00
|
|
|
def test_clientcert(self, tdata):
|
2016-06-06 04:05:44 +00:00
|
|
|
self.tval(
|
|
|
|
["get:/p/200"],
|
2018-04-22 23:05:58 +00:00
|
|
|
clientcert=tdata.path("pathod/data/clientcert/client.pem"),
|
2013-01-20 09:37:43 +00:00
|
|
|
)
|
2016-06-05 01:04:13 +00:00
|
|
|
log = self.d.log()
|
|
|
|
assert log[0]["request"]["clientcert"]["keyinfo"]
|
2013-01-20 09:37:43 +00:00
|
|
|
|
2015-06-08 13:28:24 +00:00
|
|
|
def test_http2_without_ssl(self):
|
2016-10-17 03:38:31 +00:00
|
|
|
fp = io.StringIO()
|
2015-06-08 13:28:24 +00:00
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("127.0.0.1", self.d.port),
|
2015-06-18 16:12:11 +00:00
|
|
|
use_http2=True,
|
|
|
|
ssl=False,
|
2016-06-15 05:01:19 +00:00
|
|
|
fp=fp
|
2015-06-08 13:28:24 +00:00
|
|
|
)
|
2017-02-01 15:48:46 +00:00
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
c.connect()
|
2015-06-08 13:28:24 +00:00
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
|
2016-06-05 01:04:13 +00:00
|
|
|
class TestDaemon(PathocTestDaemon):
|
2013-01-03 21:37:26 +00:00
|
|
|
ssl = False
|
2014-09-06 23:38:44 +00:00
|
|
|
|
2013-01-03 21:37:26 +00:00
|
|
|
def test_ssl_error(self):
|
2015-06-18 16:12:11 +00:00
|
|
|
c = pathoc.Pathoc(("127.0.0.1", self.d.port), ssl=True, fp=None)
|
2016-06-11 22:39:19 +00:00
|
|
|
try:
|
|
|
|
with c.connect():
|
|
|
|
pass
|
|
|
|
except Exception as e:
|
|
|
|
assert "SSL" in str(e)
|
|
|
|
else:
|
|
|
|
raise AssertionError("No exception raised.")
|
2013-01-03 21:37:26 +00:00
|
|
|
|
2014-03-02 06:04:56 +00:00
|
|
|
def test_showssl(self):
|
2016-05-29 09:43:29 +00:00
|
|
|
assert "certificate chain" not in self.tval(
|
2015-05-30 00:03:13 +00:00
|
|
|
["get:/p/200"],
|
|
|
|
showssl=True)
|
2014-03-02 06:04:56 +00:00
|
|
|
|
2012-09-26 21:44:25 +00:00
|
|
|
def test_ignorecodes(self):
|
2015-04-19 06:04:27 +00:00
|
|
|
assert "200" in self.tval(["get:'/p/200:b@1'"])
|
|
|
|
assert "200" in self.tval(["get:'/p/200:b@1'"])
|
2012-09-26 21:44:25 +00:00
|
|
|
assert "200" in self.tval(["get:'/p/200:b@1'"])
|
|
|
|
assert "200" not in self.tval(["get:'/p/200:b@1'"], ignorecodes=[200])
|
2015-05-30 00:03:13 +00:00
|
|
|
assert "200" not in self.tval(
|
|
|
|
["get:'/p/200:b@1'"],
|
|
|
|
ignorecodes=[
|
|
|
|
200,
|
|
|
|
201])
|
2012-09-26 21:44:25 +00:00
|
|
|
assert "202" in self.tval(["get:'/p/202:b@1'"], ignorecodes=[200, 201])
|
|
|
|
|
2016-06-05 01:04:13 +00:00
|
|
|
def _test_timeout(self):
|
2015-06-08 02:01:04 +00:00
|
|
|
assert "Timeout" in self.tval(["get:'/p/200:p0,100'"], timeout=0.01)
|
2015-05-30 00:03:13 +00:00
|
|
|
assert "HTTP" in self.tval(
|
2015-06-04 07:55:01 +00:00
|
|
|
["get:'/p/200:p5,100'"],
|
2015-05-30 00:03:13 +00:00
|
|
|
showresp=True,
|
2015-06-08 04:34:21 +00:00
|
|
|
timeout=1
|
2015-06-04 07:55:01 +00:00
|
|
|
)
|
2016-05-29 09:43:29 +00:00
|
|
|
assert "HTTP" not in self.tval(
|
2015-06-08 04:34:21 +00:00
|
|
|
["get:'/p/200:p3,100'"],
|
2015-05-30 00:03:13 +00:00
|
|
|
showresp=True,
|
2015-06-08 04:34:21 +00:00
|
|
|
timeout=1,
|
2015-06-04 07:55:01 +00:00
|
|
|
ignoretimeout=True
|
|
|
|
)
|
2012-09-25 23:07:22 +00:00
|
|
|
|
2012-09-25 22:38:47 +00:00
|
|
|
def test_showresp(self):
|
2016-06-05 01:04:13 +00:00
|
|
|
reqs = ["get:/p/200:da", "get:/p/200:da"]
|
|
|
|
assert self.tval(reqs).count("200 OK") == 2
|
2015-04-30 01:59:10 +00:00
|
|
|
assert self.tval(reqs, showresp=True).count("HTTP/1.1 200 OK") == 2
|
|
|
|
assert self.tval(
|
|
|
|
reqs, showresp=True, hexdump=True
|
|
|
|
).count("0000000000") == 2
|
2012-09-25 23:07:22 +00:00
|
|
|
|
|
|
|
def test_showresp_httperr(self):
|
2015-04-19 06:04:27 +00:00
|
|
|
v = self.tval(["get:'/p/200:d20'"], showresp=True, showsummary=True)
|
2016-06-04 23:47:52 +00:00
|
|
|
assert "Invalid header" in v
|
2012-09-25 23:07:22 +00:00
|
|
|
assert "HTTP/" in v
|
2012-06-29 22:51:13 +00:00
|
|
|
|
2012-10-30 22:23:53 +00:00
|
|
|
def test_explain(self):
|
2014-10-25 02:30:54 +00:00
|
|
|
reqs = ["get:/p/200:b@100"]
|
|
|
|
assert "b@100" not in self.tval(reqs, explain=True)
|
2012-10-30 22:23:53 +00:00
|
|
|
|
2012-09-25 22:38:47 +00:00
|
|
|
def test_showreq(self):
|
2016-06-05 01:04:13 +00:00
|
|
|
reqs = ["get:/p/200:da", "get:/p/200:da"]
|
|
|
|
assert self.tval(reqs, showreq=True).count("GET /p/200") == 2
|
2015-04-30 01:59:10 +00:00
|
|
|
assert self.tval(
|
|
|
|
reqs, showreq=True, hexdump=True
|
|
|
|
).count("0000000000") == 2
|
2012-09-25 22:38:47 +00:00
|
|
|
|
2012-06-29 22:51:13 +00:00
|
|
|
def test_conn_err(self):
|
|
|
|
assert "Invalid server response" in self.tval(["get:'/p/200:d2'"])
|
2012-07-22 11:37:46 +00:00
|
|
|
|
2015-06-08 04:25:33 +00:00
|
|
|
def test_websocket_shutdown(self):
|
2016-06-06 04:05:44 +00:00
|
|
|
self.tval(["ws:/"])
|
2015-06-08 04:25:33 +00:00
|
|
|
|
|
|
|
def test_wait_finish(self):
|
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("127.0.0.1", self.d.port),
|
|
|
|
fp=None,
|
2015-06-18 16:12:11 +00:00
|
|
|
ws_read_limit=1
|
2015-06-08 04:25:33 +00:00
|
|
|
)
|
2016-06-05 01:04:13 +00:00
|
|
|
with c.connect():
|
|
|
|
c.request("ws:/")
|
2016-06-06 06:17:22 +00:00
|
|
|
c.request("wf:f'wf'")
|
|
|
|
# This should read a frame and close the websocket reader
|
|
|
|
assert len([i for i in c.wait(timeout=5, finish=False)]) == 1
|
|
|
|
assert not [i for i in c.wait(timeout=0)]
|
2015-06-08 04:25:33 +00:00
|
|
|
|
2013-03-02 03:57:00 +00:00
|
|
|
def test_connect_fail(self):
|
|
|
|
to = ("foobar", 80)
|
2015-04-30 01:59:10 +00:00
|
|
|
c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None)
|
2016-10-17 03:38:31 +00:00
|
|
|
c.rfile, c.wfile = io.BytesIO(), io.BytesIO()
|
2017-02-06 16:48:44 +00:00
|
|
|
with pytest.raises(Exception, match="CONNECT failed"):
|
2015-09-16 16:44:34 +00:00
|
|
|
c.http_connect(to)
|
2016-10-17 03:38:31 +00:00
|
|
|
c.rfile = io.BytesIO(
|
2016-06-15 05:03:56 +00:00
|
|
|
b"HTTP/1.1 500 OK\r\n"
|
2013-03-02 03:57:00 +00:00
|
|
|
)
|
2017-02-06 16:48:44 +00:00
|
|
|
with pytest.raises(Exception, match="CONNECT failed"):
|
2015-09-16 16:44:34 +00:00
|
|
|
c.http_connect(to)
|
2016-10-17 03:38:31 +00:00
|
|
|
c.rfile = io.BytesIO(
|
2016-06-15 05:03:56 +00:00
|
|
|
b"HTTP/1.1 200 OK\r\n"
|
2013-03-02 03:57:00 +00:00
|
|
|
)
|
2013-12-15 05:42:58 +00:00
|
|
|
c.http_connect(to)
|
2015-06-08 13:28:24 +00:00
|
|
|
|
2015-07-03 00:48:35 +00:00
|
|
|
def test_socks_connect(self):
|
|
|
|
to = ("foobar", 80)
|
|
|
|
c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None)
|
2016-10-17 03:38:31 +00:00
|
|
|
c.rfile, c.wfile = tutils.treader(b""), io.BytesIO()
|
2017-02-01 15:48:46 +00:00
|
|
|
with pytest.raises(pathoc.PathocError):
|
|
|
|
c.socks_connect(to)
|
2015-07-03 00:48:35 +00:00
|
|
|
|
|
|
|
c.rfile = tutils.treader(
|
2016-06-15 05:03:56 +00:00
|
|
|
b"\x05\xEE"
|
2015-07-03 00:48:35 +00:00
|
|
|
)
|
2017-02-06 16:48:44 +00:00
|
|
|
with pytest.raises(Exception, match="SOCKS without authentication"):
|
2017-02-01 15:48:46 +00:00
|
|
|
c.socks_connect(("example.com", 0xDEAD))
|
2015-07-03 00:48:35 +00:00
|
|
|
|
|
|
|
c.rfile = tutils.treader(
|
2016-06-15 05:03:56 +00:00
|
|
|
b"\x05\x00" +
|
|
|
|
b"\x05\xEE\x00\x03\x0bexample.com\xDE\xAD"
|
2015-07-03 00:48:35 +00:00
|
|
|
)
|
2017-02-06 16:48:44 +00:00
|
|
|
with pytest.raises(Exception, match="SOCKS server error"):
|
2017-02-01 15:48:46 +00:00
|
|
|
c.socks_connect(("example.com", 0xDEAD))
|
2015-07-03 00:48:35 +00:00
|
|
|
|
|
|
|
c.rfile = tutils.treader(
|
2016-06-15 05:03:56 +00:00
|
|
|
b"\x05\x00" +
|
|
|
|
b"\x05\x00\x00\x03\x0bexample.com\xDE\xAD"
|
2015-07-03 00:48:35 +00:00
|
|
|
)
|
|
|
|
c.socks_connect(("example.com", 0xDEAD))
|
|
|
|
|
2015-06-08 13:28:24 +00:00
|
|
|
|
2016-06-05 01:04:13 +00:00
|
|
|
class TestDaemonHTTP2(PathocTestDaemon):
|
2015-06-08 13:28:24 +00:00
|
|
|
ssl = True
|
2016-06-05 01:24:46 +00:00
|
|
|
explain = False
|
2015-06-08 13:28:24 +00:00
|
|
|
|
2016-12-01 09:36:18 +00:00
|
|
|
def test_http2(self):
|
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("127.0.0.1", self.d.port),
|
|
|
|
fp=None,
|
|
|
|
ssl=True,
|
|
|
|
use_http2=True,
|
|
|
|
)
|
|
|
|
assert isinstance(c.protocol, HTTP2StateProtocol)
|
|
|
|
|
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("127.0.0.1", self.d.port),
|
|
|
|
)
|
|
|
|
assert c.protocol == http1
|
|
|
|
|
|
|
|
def test_http2_alpn(self):
|
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("127.0.0.1", self.d.port),
|
|
|
|
fp=None,
|
|
|
|
ssl=True,
|
|
|
|
use_http2=True,
|
|
|
|
http2_skip_connection_preface=True,
|
|
|
|
)
|
|
|
|
|
2018-01-06 09:43:33 +00:00
|
|
|
tmp_convert_to_tls = c.convert_to_tls
|
|
|
|
c.convert_to_tls = Mock()
|
|
|
|
c.convert_to_tls.side_effect = tmp_convert_to_tls
|
2016-12-01 09:36:18 +00:00
|
|
|
with c.connect():
|
2018-01-06 09:43:33 +00:00
|
|
|
_, kwargs = c.convert_to_tls.call_args
|
2016-12-01 09:36:18 +00:00
|
|
|
assert set(kwargs['alpn_protos']) == set([b'http/1.1', b'h2'])
|
|
|
|
|
|
|
|
def test_request(self):
|
|
|
|
c = pathoc.Pathoc(
|
|
|
|
("127.0.0.1", self.d.port),
|
|
|
|
fp=None,
|
|
|
|
ssl=True,
|
|
|
|
use_http2=True,
|
|
|
|
)
|
|
|
|
with c.connect():
|
|
|
|
resp = c.request("get:/p/200")
|
|
|
|
assert resp.status_code == 200
|