Test cert generation errors.

This commit is contained in:
Aldo Cortesi 2013-03-02 16:59:16 +13:00
parent 09c73019c5
commit 415844511c
4 changed files with 22 additions and 9 deletions

View File

@ -306,7 +306,7 @@ class ProxyHandler(tcp.BaseHandler):
host = conn.cert.cn.decode("utf8").encode("idna") host = conn.cert.cn.decode("utf8").encode("idna")
ret = self.config.certstore.get_cert(host, sans, self.config.cacert) ret = self.config.certstore.get_cert(host, sans, self.config.cacert)
if not ret: if not ret:
raise ProxyError(502, "mitmproxy: Unable to generate dummy cert.") raise ProxyError(502, "Unable to generate dummy cert.")
return ret return ret
def get_line(self, fp): def get_line(self, fp):

View File

@ -22,7 +22,6 @@ def test_app_registry():
r.port = 81 r.port = 81
assert not ar.get(r) assert not ar.get(r)
r = tutils.treq() r = tutils.treq()
r.host = "domain2" r.host = "domain2"
r.port = 80 r.port = 80

View File

@ -141,6 +141,16 @@ class TestHTTPS(tservers.HTTPProxTest, CommonMixin):
assert self.server.last_log()["request"]["sni"] == "testserver.com" assert self.server.last_log()["request"]["sni"] == "testserver.com"
class TestHTTPSNoUpstream(tservers.HTTPProxTest, CommonMixin):
ssl = True
no_upstream_cert = True
def test_cert_gen_error(self):
f = self.pathoc_raw()
f.connect((u"\u2102\u0001".encode("utf8"), 0))
f.request("get:/")
assert "dummy cert" in "".join(self.proxy.log)
class TestHTTPSCertfile(tservers.HTTPProxTest, CommonMixin): class TestHTTPSCertfile(tservers.HTTPProxTest, CommonMixin):
ssl = True ssl = True
certfile = True certfile = True
@ -227,7 +237,6 @@ class MasterFakeResponse(tservers.TestMaster):
class TestFakeResponse(tservers.HTTPProxTest): class TestFakeResponse(tservers.HTTPProxTest):
masterclass = MasterFakeResponse masterclass = MasterFakeResponse
def test_kill(self): def test_kill(self):
p = self.pathoc()
f = self.pathod("200") f = self.pathod("200")
assert "header_response" in f.headers.keys() assert "header_response" in f.headers.keys()
@ -241,7 +250,6 @@ class MasterKillRequest(tservers.TestMaster):
class TestKillRequest(tservers.HTTPProxTest): class TestKillRequest(tservers.HTTPProxTest):
masterclass = MasterKillRequest masterclass = MasterKillRequest
def test_kill(self): def test_kill(self):
p = self.pathoc()
tutils.raises("server disconnect", self.pathod, "200") tutils.raises("server disconnect", self.pathod, "200")
# Nothing should have hit the server # Nothing should have hit the server
assert not self.server.last_log() assert not self.server.last_log()
@ -255,7 +263,6 @@ class MasterKillResponse(tservers.TestMaster):
class TestKillResponse(tservers.HTTPProxTest): class TestKillResponse(tservers.HTTPProxTest):
masterclass = MasterKillResponse masterclass = MasterKillResponse
def test_kill(self): def test_kill(self):
p = self.pathoc()
tutils.raises("server disconnect", self.pathod, "200") tutils.raises("server disconnect", self.pathod, "200")
# The server should have seen a request # The server should have seen a request
assert self.server.last_log() assert self.server.last_log()

View File

@ -71,6 +71,7 @@ class ProxTestBase:
ssl = None ssl = None
clientcerts = False clientcerts = False
certfile = None certfile = None
no_upstream_cert = False
masterclass = TestMaster masterclass = TestMaster
@classmethod @classmethod
@ -80,6 +81,7 @@ class ProxTestBase:
cls.server2 = libpathod.test.Daemon(ssl=cls.ssl) cls.server2 = libpathod.test.Daemon(ssl=cls.ssl)
pconf = cls.get_proxy_config() pconf = cls.get_proxy_config()
config = proxy.ProxyConfig( config = proxy.ProxyConfig(
no_upstream_cert = cls.no_upstream_cert,
cacert = tutils.test_data.path("data/serverkey.pem"), cacert = tutils.test_data.path("data/serverkey.pem"),
**pconf **pconf
) )
@ -127,23 +129,28 @@ class ProxTestBase:
class HTTPProxTest(ProxTestBase): class HTTPProxTest(ProxTestBase):
def pathoc(self, connect_to = None, sni=None): def pathoc_raw(self):
return libpathod.pathoc.Pathoc("127.0.0.1", self.proxy.port)
def pathoc(self, sni=None):
""" """
Returns a connected Pathoc instance. Returns a connected Pathoc instance.
""" """
p = libpathod.pathoc.Pathoc("localhost", self.proxy.port, ssl=self.ssl, sni=sni) p = libpathod.pathoc.Pathoc("localhost", self.proxy.port, ssl=self.ssl, sni=sni)
p.connect(connect_to) if self.ssl:
p.connect(("127.0.0.1", self.server.port))
else:
p.connect()
return p return p
def pathod(self, spec, sni=None): def pathod(self, spec, sni=None):
""" """
Constructs a pathod GET request, with the appropriate base and proxy. Constructs a pathod GET request, with the appropriate base and proxy.
""" """
p = self.pathoc(sni=sni)
if self.ssl: if self.ssl:
p = self.pathoc(("127.0.0.1", self.server.port), sni=sni)
q = "get:'/p/%s'"%spec q = "get:'/p/%s'"%spec
else: else:
p = self.pathoc()
q = "get:'%s/p/%s'"%(self.server.urlbase, spec) q = "get:'%s/p/%s'"%(self.server.urlbase, spec)
return p.request(q) return p.request(q)