2016-03-20 18:40:03 +00:00
|
|
|
from six.moves import cStringIO as StringIO
|
2015-06-18 08:36:58 +00:00
|
|
|
|
2016-06-02 06:27:51 +00:00
|
|
|
from pathod import pathod
|
2016-05-28 12:36:43 +00:00
|
|
|
from netlib import tcp
|
2015-09-17 00:13:42 +00:00
|
|
|
from netlib.exceptions import HttpException, TlsException
|
2012-06-24 04:38:32 +00:00
|
|
|
import tutils
|
2012-04-28 22:56:33 +00:00
|
|
|
|
2014-10-24 01:01:34 +00:00
|
|
|
|
2015-04-19 20:56:47 +00:00
|
|
|
class TestPathod(object):
|
2015-06-18 16:12:11 +00:00
|
|
|
|
2012-06-21 02:29:49 +00:00
|
|
|
def test_logging(self):
|
2016-03-20 18:40:03 +00:00
|
|
|
s = 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
|
|
|
|
|
|
|
|
2012-09-30 23:01:02 +00:00
|
|
|
class TestTimeout(tutils.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.
|
2015-05-31 06:38:11 +00:00
|
|
|
tutils.raises(Exception, self.pathoc, ["get:/:p1,1"])
|
2012-09-30 23:01:02 +00:00
|
|
|
assert self.d.last_log()["type"] == "timeout"
|
|
|
|
|
|
|
|
|
2013-01-05 07:36:06 +00:00
|
|
|
class TestNotAfterConnect(tutils.DaemonTests):
|
|
|
|
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
|
|
|
|
|
|
|
|
2014-03-02 02:13:56 +00:00
|
|
|
class TestCustomCert(tutils.DaemonTests):
|
|
|
|
ssl = True
|
|
|
|
ssloptions = dict(
|
2015-06-18 16:12:11 +00:00
|
|
|
certs=[("*", tutils.test_data.path("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
|
|
|
|
|
|
|
|
2014-03-02 00:45:35 +00:00
|
|
|
class TestSSLCN(tutils.DaemonTests):
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
2012-07-26 08:01:51 +00:00
|
|
|
class TestNohang(tutils.DaemonTests):
|
|
|
|
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
|
|
|
|
|
|
|
|
2012-10-24 21:59:18 +00:00
|
|
|
class TestHexdump(tutils.DaemonTests):
|
|
|
|
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
|
|
|
|
|
|
|
|
2015-06-04 08:36:50 +00:00
|
|
|
class TestNocraft(tutils.DaemonTests):
|
|
|
|
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
|
|
|
|
|
|
|
|
2012-07-23 04:18:47 +00:00
|
|
|
class CommonTests(tutils.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):
|
2016-06-02 07:32:50 +00:00
|
|
|
tutils.raises("unexpected eof", 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):
|
2014-10-24 01:01:34 +00:00
|
|
|
tutils.raises(
|
2015-09-16 16:44:34 +00:00
|
|
|
HttpException,
|
2014-10-24 01:01:34 +00:00
|
|
|
self.pathoc,
|
2015-05-31 06:38:11 +00:00
|
|
|
["get:/:h'content-length'='foo'"]
|
2014-10-24 01:01:34 +00:00
|
|
|
)
|
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):
|
2015-09-16 16:44:34 +00:00
|
|
|
tutils.raises(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):
|
2014-03-02 00:45:35 +00:00
|
|
|
tutils.raises(
|
2015-09-16 16:44:34 +00:00
|
|
|
HttpException,
|
2014-03-02 00:45:35 +00:00
|
|
|
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)
|
|
|
|
)
|
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")
|
2016-06-06 23:03:45 +00:00
|
|
|
tutils.raises(TlsException, c.convert_to_ssl)
|
|
|
|
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
|
|
|
|
2015-06-16 11:52:41 +00:00
|
|
|
class TestHTTP2(tutils.DaemonTests):
|
|
|
|
ssl = True
|
|
|
|
nohang = True
|
|
|
|
|
2016-02-15 17:43:06 +00:00
|
|
|
if tcp.HAS_ALPN:
|
2015-06-18 08:36:58 +00:00
|
|
|
|
|
|
|
def test_http2(self):
|
|
|
|
r, _ = self.pathoc(["GET:/"], ssl=True, use_http2=True)
|
2015-07-29 09:26:10 +00:00
|
|
|
assert r[0].status_code == 800
|