2013-03-02 22:58:57 +00:00
|
|
|
import argparse
|
2014-03-09 20:51:24 +00:00
|
|
|
from libmproxy import cmdline
|
|
|
|
from libmproxy.proxy.config import process_proxy_options
|
|
|
|
from libmproxy.proxy.connection import ServerConnection
|
|
|
|
from libmproxy.proxy.primitives import ProxyError
|
2014-09-06 11:09:57 +00:00
|
|
|
from libmproxy.proxy.server import DummyServer, ProxyServer, ConnectionHandler
|
2012-06-09 01:42:43 +00:00
|
|
|
import tutils
|
2013-01-06 03:44:12 +00:00
|
|
|
from libpathod import test
|
2013-01-28 22:35:57 +00:00
|
|
|
from netlib import http, tcp
|
2013-01-28 08:59:03 +00:00
|
|
|
import mock
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2012-06-10 04:49:59 +00:00
|
|
|
|
2012-07-08 22:58:28 +00:00
|
|
|
def test_proxy_error():
|
2014-03-09 20:13:08 +00:00
|
|
|
p = ProxyError(111, "msg")
|
2012-07-08 22:58:28 +00:00
|
|
|
assert str(p)
|
2010-02-16 04:09:07 +00:00
|
|
|
|
2012-06-18 22:42:55 +00:00
|
|
|
|
2013-01-06 03:44:12 +00:00
|
|
|
class TestServerConnection:
|
|
|
|
def setUp(self):
|
|
|
|
self.d = test.Daemon()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.d.shutdown()
|
|
|
|
|
|
|
|
def test_simple(self):
|
2014-03-09 20:13:08 +00:00
|
|
|
sc = ServerConnection((self.d.IFACE, self.d.port), None)
|
2013-02-24 09:24:21 +00:00
|
|
|
sc.connect()
|
2013-01-06 03:44:12 +00:00
|
|
|
r = tutils.treq()
|
2014-02-05 13:33:17 +00:00
|
|
|
r.flow.server_conn = sc
|
2013-01-06 03:44:12 +00:00
|
|
|
r.path = "/p/200:da"
|
2014-01-29 01:49:11 +00:00
|
|
|
sc.send(r._assemble())
|
2013-01-06 03:44:12 +00:00
|
|
|
assert http.read_response(sc.rfile, r.method, 1000)
|
|
|
|
assert self.d.last_log()
|
|
|
|
|
2014-01-29 01:49:11 +00:00
|
|
|
sc.finish()
|
2013-01-28 08:59:03 +00:00
|
|
|
|
|
|
|
def test_terminate_error(self):
|
2014-03-09 20:13:08 +00:00
|
|
|
sc = ServerConnection((self.d.IFACE, self.d.port), None)
|
2013-02-24 09:24:21 +00:00
|
|
|
sc.connect()
|
2013-01-28 08:59:03 +00:00
|
|
|
sc.connection = mock.Mock()
|
2014-01-29 01:49:11 +00:00
|
|
|
sc.connection.recv = mock.Mock(return_value=False)
|
2013-07-29 21:42:29 +00:00
|
|
|
sc.connection.flush = mock.Mock(side_effect=tcp.NetLibDisconnect)
|
2014-01-29 01:49:11 +00:00
|
|
|
sc.finish()
|
2013-01-28 22:35:57 +00:00
|
|
|
|
2013-03-02 21:37:06 +00:00
|
|
|
|
2013-03-02 22:58:57 +00:00
|
|
|
class TestProcessProxyOptions:
|
|
|
|
def p(self, *args):
|
2014-03-10 04:11:51 +00:00
|
|
|
parser = tutils.MockParser()
|
2013-03-02 22:58:57 +00:00
|
|
|
cmdline.common_options(parser)
|
|
|
|
opts = parser.parse_args(args=args)
|
2014-03-10 04:11:51 +00:00
|
|
|
return parser, process_proxy_options(parser, opts)
|
2013-03-02 22:58:57 +00:00
|
|
|
|
|
|
|
def assert_err(self, err, *args):
|
2014-03-10 04:11:51 +00:00
|
|
|
tutils.raises(err, self.p, *args)
|
2013-03-02 22:58:57 +00:00
|
|
|
|
|
|
|
def assert_noerr(self, *args):
|
|
|
|
m, p = self.p(*args)
|
|
|
|
assert p
|
|
|
|
return p
|
|
|
|
|
|
|
|
def test_simple(self):
|
|
|
|
assert self.p()
|
|
|
|
|
|
|
|
def test_confdir(self):
|
|
|
|
with tutils.tmpdir() as confdir:
|
|
|
|
self.assert_noerr("--confdir", confdir)
|
|
|
|
|
|
|
|
@mock.patch("libmproxy.platform.resolver", None)
|
|
|
|
def test_no_transparent(self):
|
|
|
|
self.assert_err("transparent mode not supported", "-T")
|
|
|
|
|
2014-09-06 10:23:05 +00:00
|
|
|
|
2013-03-02 22:58:57 +00:00
|
|
|
@mock.patch("libmproxy.platform.resolver")
|
2014-09-06 10:23:05 +00:00
|
|
|
def test_modes(self, _):
|
2014-03-10 04:11:51 +00:00
|
|
|
self.assert_noerr("-R", "http://localhost")
|
2014-09-06 10:23:05 +00:00
|
|
|
self.assert_err("expected one argument", "-R")
|
|
|
|
self.assert_err("Invalid server specification", "-R", "reverse")
|
|
|
|
|
|
|
|
self.assert_noerr("-T")
|
|
|
|
|
|
|
|
self.assert_noerr("-U", "http://localhost")
|
|
|
|
self.assert_err("expected one argument", "-U")
|
|
|
|
self.assert_err("Invalid server specification", "-U", "upstream")
|
|
|
|
|
|
|
|
self.assert_noerr("--destination-server", "http://localhost")
|
|
|
|
self.assert_err("expected one argument", "--destination-server")
|
|
|
|
self.assert_err("Invalid server specification", "--destination-server", "manual")
|
|
|
|
|
|
|
|
self.assert_err("mutually exclusive", "-R", "http://localhost", "-T")
|
2013-03-02 22:58:57 +00:00
|
|
|
|
2014-03-05 04:25:12 +00:00
|
|
|
def test_client_certs(self):
|
2013-03-02 22:58:57 +00:00
|
|
|
with tutils.tmpdir() as confdir:
|
|
|
|
self.assert_noerr("--client-certs", confdir)
|
|
|
|
self.assert_err("directory does not exist", "--client-certs", "nonexistent")
|
|
|
|
|
2014-03-05 04:25:12 +00:00
|
|
|
def test_certs(self):
|
|
|
|
with tutils.tmpdir() as confdir:
|
|
|
|
self.assert_noerr("--cert", tutils.test_data.path("data/testkey.pem"))
|
|
|
|
self.assert_err("does not exist", "--cert", "nonexistent")
|
|
|
|
|
2013-03-02 22:58:57 +00:00
|
|
|
def test_auth(self):
|
|
|
|
p = self.assert_noerr("--nonanonymous")
|
|
|
|
assert p.authenticator
|
|
|
|
|
|
|
|
p = self.assert_noerr("--htpasswd", tutils.test_data.path("data/htpasswd"))
|
|
|
|
assert p.authenticator
|
2014-08-17 21:06:25 +00:00
|
|
|
self.assert_err("malformed htpasswd file", "--htpasswd", tutils.test_data.path("data/htpasswd.invalid"))
|
2013-03-02 22:58:57 +00:00
|
|
|
|
|
|
|
p = self.assert_noerr("--singleuser", "test:test")
|
|
|
|
assert p.authenticator
|
|
|
|
self.assert_err("invalid single-user specification", "--singleuser", "test")
|
|
|
|
|
2013-03-02 21:37:06 +00:00
|
|
|
|
2013-03-02 23:13:33 +00:00
|
|
|
class TestProxyServer:
|
2013-06-17 22:53:38 +00:00
|
|
|
@tutils.SkipWindows # binding to 0.0.0.0:1 works without special permissions on Windows
|
2013-03-02 23:13:33 +00:00
|
|
|
def test_err(self):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
cmdline.common_options(parser)
|
|
|
|
opts = parser.parse_args(args=[])
|
2014-03-09 20:51:24 +00:00
|
|
|
tutils.raises("error starting proxy server", ProxyServer, opts, 1)
|
2013-03-02 23:13:33 +00:00
|
|
|
|
2014-09-06 11:09:57 +00:00
|
|
|
def test_err_2(self):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
cmdline.common_options(parser)
|
|
|
|
opts = parser.parse_args(args=[])
|
|
|
|
tutils.raises("error starting proxy server", ProxyServer, opts, 8080, "invalidhost")
|
|
|
|
|
2013-03-02 23:13:33 +00:00
|
|
|
|
|
|
|
class TestDummyServer:
|
|
|
|
def test_simple(self):
|
2014-03-09 20:51:24 +00:00
|
|
|
d = DummyServer(None)
|
2013-03-02 23:13:33 +00:00
|
|
|
d.start_slave()
|
|
|
|
d.shutdown()
|
|
|
|
|
2014-09-06 11:09:57 +00:00
|
|
|
|
|
|
|
class TestConnectionHandler:
|
|
|
|
def test_fatal_error(self):
|
|
|
|
config = dict(get_upstream_server=mock.Mock(side_effect=RuntimeError))
|
|
|
|
c = ConnectionHandler(config, mock.MagicMock(), ("127.0.0.1", 8080), None, mock.MagicMock(), None)
|
|
|
|
with tutils.capture_stderr(c.handle) as output:
|
|
|
|
assert "mitmproxy has crashed" in output
|