mitmproxy/test/test_tcp.py

165 lines
4.1 KiB
Python
Raw Normal View History

2012-06-18 21:42:32 +00:00
import cStringIO, threading, Queue
from netlib import tcp
import tutils
class ServerThread(threading.Thread):
def __init__(self, server):
self.server = server
threading.Thread.__init__(self)
def run(self):
self.server.serve_forever()
def shutdown(self):
self.server.shutdown()
class ServerTestBase:
@classmethod
def setupAll(cls):
cls.server = ServerThread(cls.makeserver())
cls.server.start()
@classmethod
def teardownAll(cls):
cls.server.shutdown()
2012-06-25 21:50:42 +00:00
class SNIHandler(tcp.BaseHandler):
sni = None
def handle_sni(self, connection):
self.sni = connection.get_servername()
def handle(self):
self.wfile.write(self.sni)
self.wfile.flush()
class EchoHandler(tcp.BaseHandler):
2012-06-25 21:50:42 +00:00
sni = None
def handle_sni(self, connection):
self.sni = connection.get_servername()
2012-06-18 21:42:32 +00:00
def handle(self):
v = self.rfile.readline()
if v.startswith("echo"):
self.wfile.write(v)
elif v.startswith("error"):
raise ValueError("Testing an error.")
self.wfile.flush()
class DisconnectHandler(tcp.BaseHandler):
def handle(self):
self.finish()
2012-06-18 21:42:32 +00:00
class TServer(tcp.TCPServer):
def __init__(self, addr, ssl, q, handler):
2012-06-18 21:42:32 +00:00
tcp.TCPServer.__init__(self, addr)
self.ssl, self.q = ssl, q
self.handler = handler
2012-06-18 21:42:32 +00:00
def handle_connection(self, request, client_address):
h = self.handler(request, client_address, self)
if self.ssl:
h.convert_to_ssl(
tutils.test_data.path("data/server.crt"),
tutils.test_data.path("data/server.key"),
)
h.handle()
h.finish()
2012-06-18 21:42:32 +00:00
def handle_error(self, request, client_address):
s = cStringIO.StringIO()
tcp.TCPServer.handle_error(self, request, client_address, s)
self.q.put(s.getvalue())
class TestServer(ServerTestBase):
@classmethod
def makeserver(cls):
cls.q = Queue.Queue()
s = TServer(("127.0.0.1", 0), False, cls.q, EchoHandler)
2012-06-18 21:42:32 +00:00
cls.port = s.port
return s
def test_echo(self):
testval = "echo!\n"
c = tcp.TCPClient("127.0.0.1", self.port)
c.connect()
2012-06-18 21:42:32 +00:00
c.wfile.write(testval)
c.wfile.flush()
assert c.rfile.readline() == testval
class TestServerSSL(ServerTestBase):
@classmethod
def makeserver(cls):
cls.q = Queue.Queue()
s = TServer(("127.0.0.1", 0), True, cls.q, EchoHandler)
cls.port = s.port
return s
def test_echo(self):
c = tcp.TCPClient("127.0.0.1", self.port)
c.connect()
2012-06-25 21:50:42 +00:00
c.convert_to_ssl(sni="foo.com")
testval = "echo!\n"
2012-06-18 21:42:32 +00:00
c.wfile.write(testval)
c.wfile.flush()
assert c.rfile.readline() == testval
2012-06-18 21:42:32 +00:00
2012-06-25 21:50:42 +00:00
class TestSNI(ServerTestBase):
@classmethod
def makeserver(cls):
cls.q = Queue.Queue()
s = TServer(("127.0.0.1", 0), True, cls.q, SNIHandler)
cls.port = s.port
return s
def test_echo(self):
c = tcp.TCPClient("127.0.0.1", self.port)
c.connect()
c.convert_to_ssl(sni="foo.com")
assert c.rfile.readline() == "foo.com"
class TestSSLDisconnect(ServerTestBase):
@classmethod
def makeserver(cls):
cls.q = Queue.Queue()
s = TServer(("127.0.0.1", 0), True, cls.q, DisconnectHandler)
cls.port = s.port
return s
def test_echo(self):
c = tcp.TCPClient("127.0.0.1", self.port)
c.connect()
c.convert_to_ssl()
# Excercise SSL.ZeroReturnError
c.rfile.read(10)
2012-06-18 21:42:32 +00:00
class TestTCPClient:
def test_conerr(self):
c = tcp.TCPClient("127.0.0.1", 0)
tutils.raises(tcp.NetLibError, c.connect)
2012-06-18 21:42:32 +00:00
class TestFileLike:
def test_wrap(self):
s = cStringIO.StringIO("foobar\nfoobar")
s = tcp.FileLike(s)
s.flush()
assert s.readline() == "foobar\n"
assert s.readline() == "foobar"
# Test __getattr__
assert s.isatty
def test_limit(self):
s = cStringIO.StringIO("foobar\nfoobar")
s = tcp.FileLike(s)
assert s.readline(3) == "foo"