2012-07-21 04:10:54 +00:00
|
|
|
import cStringIO, threading, Queue, time
|
2012-06-27 20:15:55 +00:00
|
|
|
from netlib import tcp, certutils
|
2012-06-18 21:42:32 +00:00
|
|
|
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):
|
2012-07-04 21:37:43 +00:00
|
|
|
cls.q = Queue.Queue()
|
|
|
|
s = cls.makeserver()
|
|
|
|
cls.port = s.port
|
|
|
|
cls.server = ServerThread(s)
|
2012-06-18 21:42:32 +00:00
|
|
|
cls.server.start()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def teardownAll(cls):
|
|
|
|
cls.server.shutdown()
|
|
|
|
|
|
|
|
|
2012-09-30 22:30:02 +00:00
|
|
|
@property
|
|
|
|
def last_handler(self):
|
|
|
|
return self.server.server.last_handler
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2012-06-25 04:16:01 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
2013-01-20 09:13:38 +00:00
|
|
|
class CertHandler(tcp.BaseHandler):
|
|
|
|
sni = None
|
|
|
|
def handle_sni(self, connection):
|
|
|
|
self.sni = connection.get_servername()
|
|
|
|
|
|
|
|
def handle(self):
|
|
|
|
self.wfile.write("%s\n"%self.clientcert.serial)
|
|
|
|
self.wfile.flush()
|
|
|
|
|
|
|
|
|
2012-06-25 04:16:01 +00:00
|
|
|
class DisconnectHandler(tcp.BaseHandler):
|
|
|
|
def handle(self):
|
2012-07-20 02:43:51 +00:00
|
|
|
self.close()
|
2012-06-25 04:16:01 +00:00
|
|
|
|
|
|
|
|
2012-07-21 04:10:54 +00:00
|
|
|
class HangHandler(tcp.BaseHandler):
|
|
|
|
def handle(self):
|
|
|
|
while 1:
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
2012-09-30 22:30:02 +00:00
|
|
|
class TimeoutHandler(tcp.BaseHandler):
|
|
|
|
def handle(self):
|
|
|
|
self.timeout = False
|
|
|
|
self.settimeout(0.01)
|
|
|
|
try:
|
|
|
|
self.rfile.read(10)
|
|
|
|
except tcp.NetLibTimeout:
|
|
|
|
self.timeout = True
|
|
|
|
|
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
class TServer(tcp.TCPServer):
|
2012-09-30 22:30:02 +00:00
|
|
|
def __init__(self, addr, ssl, q, handler_klass, v3_only=False):
|
2012-06-18 21:42:32 +00:00
|
|
|
tcp.TCPServer.__init__(self, addr)
|
2012-06-24 23:00:39 +00:00
|
|
|
self.ssl, self.q = ssl, q
|
2012-07-04 09:30:07 +00:00
|
|
|
self.v3_only = v3_only
|
2012-09-30 22:30:02 +00:00
|
|
|
self.handler_klass = handler_klass
|
|
|
|
self.last_handler = None
|
2012-06-18 21:42:32 +00:00
|
|
|
|
|
|
|
def handle_connection(self, request, client_address):
|
2012-09-30 22:30:02 +00:00
|
|
|
h = self.handler_klass(request, client_address, self)
|
|
|
|
self.last_handler = h
|
2012-06-25 04:16:01 +00:00
|
|
|
if self.ssl:
|
2012-07-04 09:30:07 +00:00
|
|
|
if self.v3_only:
|
|
|
|
method = tcp.SSLv3_METHOD
|
|
|
|
options = tcp.OP_NO_SSLv2|tcp.OP_NO_TLSv1
|
|
|
|
else:
|
|
|
|
method = tcp.SSLv23_METHOD
|
|
|
|
options = None
|
2012-06-25 04:16:01 +00:00
|
|
|
h.convert_to_ssl(
|
|
|
|
tutils.test_data.path("data/server.crt"),
|
|
|
|
tutils.test_data.path("data/server.key"),
|
2012-07-04 09:30:07 +00:00
|
|
|
method = method,
|
|
|
|
options = options,
|
2012-06-25 04:16:01 +00:00
|
|
|
)
|
2012-06-24 23:23:04 +00:00
|
|
|
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):
|
2012-07-04 21:37:43 +00:00
|
|
|
return TServer(("127.0.0.1", 0), False, cls.q, EchoHandler)
|
2012-06-18 21:42:32 +00:00
|
|
|
|
|
|
|
def test_echo(self):
|
|
|
|
testval = "echo!\n"
|
2012-06-25 02:42:15 +00:00
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
2012-06-24 23:23:04 +00:00
|
|
|
c.connect()
|
2012-06-18 21:42:32 +00:00
|
|
|
c.wfile.write(testval)
|
|
|
|
c.wfile.flush()
|
|
|
|
assert c.rfile.readline() == testval
|
|
|
|
|
2012-06-24 23:00:39 +00:00
|
|
|
|
2012-07-20 02:43:51 +00:00
|
|
|
class TestDisconnect(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
|
|
|
return TServer(("127.0.0.1", 0), False, cls.q, EchoHandler)
|
|
|
|
|
|
|
|
def test_echo(self):
|
|
|
|
testval = "echo!\n"
|
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
|
|
|
c.connect()
|
|
|
|
c.wfile.write(testval)
|
|
|
|
c.wfile.flush()
|
|
|
|
assert c.rfile.readline() == testval
|
|
|
|
|
|
|
|
|
2012-06-24 23:00:39 +00:00
|
|
|
class TestServerSSL(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
2012-07-04 21:37:43 +00:00
|
|
|
return TServer(("127.0.0.1", 0), True, cls.q, EchoHandler)
|
2012-06-24 23:00:39 +00:00
|
|
|
|
|
|
|
def test_echo(self):
|
2012-06-25 02:42:15 +00:00
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
2012-06-24 23:23:04 +00:00
|
|
|
c.connect()
|
2012-06-25 21:50:42 +00:00
|
|
|
c.convert_to_ssl(sni="foo.com")
|
2012-06-24 23:00:39 +00:00
|
|
|
testval = "echo!\n"
|
2012-06-18 21:42:32 +00:00
|
|
|
c.wfile.write(testval)
|
|
|
|
c.wfile.flush()
|
2012-06-24 23:00:39 +00:00
|
|
|
assert c.rfile.readline() == testval
|
2012-06-18 21:42:32 +00:00
|
|
|
|
2012-06-27 20:15:55 +00:00
|
|
|
def test_get_remote_cert(self):
|
|
|
|
assert certutils.get_remote_cert("127.0.0.1", self.port, None).digest("sha1")
|
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
|
2012-07-04 09:30:07 +00:00
|
|
|
class TestSSLv3Only(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
2012-07-04 21:37:43 +00:00
|
|
|
return TServer(("127.0.0.1", 0), True, cls.q, EchoHandler, True)
|
2012-07-04 09:30:07 +00:00
|
|
|
|
|
|
|
def test_failure(self):
|
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
|
|
|
c.connect()
|
|
|
|
tutils.raises(tcp.NetLibError, c.convert_to_ssl, sni="foo.com", method=tcp.TLSv1_METHOD)
|
|
|
|
|
|
|
|
|
2013-01-20 09:13:38 +00:00
|
|
|
class TestSSLClientCert(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
|
|
|
return TServer(("127.0.0.1", 0), True, cls.q, CertHandler)
|
|
|
|
|
|
|
|
def test_clientcert(self):
|
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl(clientcert=tutils.test_data.path("data/clientcert/client.pem"))
|
|
|
|
assert c.rfile.readline().strip() == "1"
|
|
|
|
|
2013-01-20 09:36:54 +00:00
|
|
|
def test_clientcert_err(self):
|
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
|
|
|
c.connect()
|
|
|
|
tutils.raises(
|
|
|
|
tcp.NetLibError,
|
|
|
|
c.convert_to_ssl,
|
|
|
|
clientcert=tutils.test_data.path("data/clientcert/make")
|
|
|
|
)
|
|
|
|
|
2013-01-20 09:13:38 +00:00
|
|
|
|
2012-06-25 21:50:42 +00:00
|
|
|
class TestSNI(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
2012-07-04 21:37:43 +00:00
|
|
|
return TServer(("127.0.0.1", 0), True, cls.q, SNIHandler)
|
2012-06-25 21:50:42 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
2012-06-25 04:16:01 +00:00
|
|
|
class TestSSLDisconnect(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
2012-07-04 21:37:43 +00:00
|
|
|
return TServer(("127.0.0.1", 0), True, cls.q, DisconnectHandler)
|
2012-06-25 04:16:01 +00:00
|
|
|
|
|
|
|
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-07-20 02:43:51 +00:00
|
|
|
c.close()
|
|
|
|
tutils.raises(tcp.NetLibDisconnect, c.wfile.write, "foo")
|
|
|
|
tutils.raises(Queue.Empty, self.q.get_nowait)
|
|
|
|
|
|
|
|
|
|
|
|
class TestDisconnect(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
|
|
|
return TServer(("127.0.0.1", 0), False, cls.q, DisconnectHandler)
|
|
|
|
|
|
|
|
def test_echo(self):
|
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
|
|
|
c.connect()
|
|
|
|
# Excercise SSL.ZeroReturnError
|
|
|
|
c.rfile.read(10)
|
|
|
|
c.wfile.write("foo")
|
|
|
|
c.close()
|
|
|
|
c.close()
|
2012-06-25 04:16:01 +00:00
|
|
|
|
|
|
|
|
2012-09-30 22:30:02 +00:00
|
|
|
class TestServerTimeOut(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
|
|
|
return TServer(("127.0.0.1", 0), False, cls.q, TimeoutHandler)
|
|
|
|
|
|
|
|
def test_timeout(self):
|
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
|
|
|
c.connect()
|
|
|
|
time.sleep(0.3)
|
|
|
|
assert self.last_handler.timeout
|
|
|
|
|
|
|
|
|
2012-07-21 04:10:54 +00:00
|
|
|
class TestTimeOut(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
|
|
|
return TServer(("127.0.0.1", 0), False, cls.q, HangHandler)
|
|
|
|
|
2012-09-30 22:30:02 +00:00
|
|
|
def test_timeout(self):
|
2012-07-21 04:10:54 +00:00
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
|
|
|
c.connect()
|
|
|
|
c.settimeout(0.1)
|
|
|
|
tutils.raises(tcp.NetLibTimeout, c.rfile.read, 10)
|
|
|
|
|
|
|
|
|
|
|
|
class TestSSLTimeOut(ServerTestBase):
|
|
|
|
@classmethod
|
|
|
|
def makeserver(cls):
|
|
|
|
return TServer(("127.0.0.1", 0), True, cls.q, HangHandler)
|
|
|
|
|
|
|
|
def test_timeout_client(self):
|
|
|
|
c = tcp.TCPClient("127.0.0.1", self.port)
|
|
|
|
c.connect()
|
|
|
|
c.convert_to_ssl()
|
|
|
|
c.settimeout(0.1)
|
|
|
|
tutils.raises(tcp.NetLibTimeout, c.rfile.read, 10)
|
|
|
|
|
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
class TestTCPClient:
|
|
|
|
def test_conerr(self):
|
2012-06-25 02:42:15 +00:00
|
|
|
c = tcp.TCPClient("127.0.0.1", 0)
|
2012-06-24 23:23:04 +00:00
|
|
|
tutils.raises(tcp.NetLibError, c.connect)
|
2012-06-18 21:42:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestFileLike:
|
2012-10-09 03:25:15 +00:00
|
|
|
def test_blocksize(self):
|
|
|
|
s = cStringIO.StringIO("1234567890abcdefghijklmnopqrstuvwxyz")
|
|
|
|
s = tcp.Reader(s)
|
|
|
|
s.BLOCKSIZE = 2
|
|
|
|
assert s.read(1) == "1"
|
|
|
|
assert s.read(2) == "23"
|
|
|
|
assert s.read(3) == "456"
|
|
|
|
assert s.read(4) == "7890"
|
|
|
|
d = s.read(-1)
|
|
|
|
assert d.startswith("abc") and d.endswith("xyz")
|
|
|
|
|
2012-06-18 21:42:32 +00:00
|
|
|
def test_wrap(self):
|
|
|
|
s = cStringIO.StringIO("foobar\nfoobar")
|
|
|
|
s.flush()
|
2012-09-23 23:10:21 +00:00
|
|
|
s = tcp.Reader(s)
|
2012-06-18 21:42:32 +00:00
|
|
|
assert s.readline() == "foobar\n"
|
|
|
|
assert s.readline() == "foobar"
|
|
|
|
# Test __getattr__
|
|
|
|
assert s.isatty
|
|
|
|
|
|
|
|
def test_limit(self):
|
|
|
|
s = cStringIO.StringIO("foobar\nfoobar")
|
2012-09-23 23:10:21 +00:00
|
|
|
s = tcp.Reader(s)
|
2012-06-18 21:42:32 +00:00
|
|
|
assert s.readline(3) == "foo"
|
2012-07-23 23:39:49 +00:00
|
|
|
|
|
|
|
def test_limitless(self):
|
|
|
|
s = cStringIO.StringIO("f"*(50*1024))
|
2012-09-23 23:10:21 +00:00
|
|
|
s = tcp.Reader(s)
|
2012-07-23 23:39:49 +00:00
|
|
|
ret = s.read(-1)
|
|
|
|
assert len(ret) == 50 * 1024
|
2012-09-23 23:10:21 +00:00
|
|
|
|
|
|
|
def test_readlog(self):
|
|
|
|
s = cStringIO.StringIO("foobar\nfoobar")
|
|
|
|
s = tcp.Reader(s)
|
|
|
|
assert not s.is_logging()
|
|
|
|
s.start_log()
|
|
|
|
assert s.is_logging()
|
|
|
|
s.readline()
|
|
|
|
assert s.get_log() == "foobar\n"
|
|
|
|
s.read(1)
|
|
|
|
assert s.get_log() == "foobar\nf"
|
|
|
|
s.start_log()
|
|
|
|
assert s.get_log() == ""
|
|
|
|
s.read(1)
|
|
|
|
assert s.get_log() == "o"
|
|
|
|
s.stop_log()
|
|
|
|
tutils.raises(ValueError, s.get_log)
|
|
|
|
|
|
|
|
def test_writelog(self):
|
|
|
|
s = cStringIO.StringIO()
|
|
|
|
s = tcp.Writer(s)
|
|
|
|
s.start_log()
|
|
|
|
assert s.is_logging()
|
|
|
|
s.write("x")
|
|
|
|
assert s.get_log() == "x"
|
|
|
|
s.write("x")
|
|
|
|
assert s.get_log() == "xx"
|
|
|
|
|
2013-01-16 20:30:19 +00:00
|
|
|
def test_reset_timestamps(self):
|
|
|
|
s = cStringIO.StringIO("foobar\nfoobar")
|
|
|
|
s = tcp.Reader(s)
|
|
|
|
s.first_byte_timestamp = 500
|
|
|
|
s.reset_timestamps()
|
|
|
|
assert not s.first_byte_timestamp
|
|
|
|
|
|
|
|
def test_first_byte_timestamp_updated_on_read(self):
|
|
|
|
s = cStringIO.StringIO("foobar\nfoobar")
|
|
|
|
s = tcp.Reader(s)
|
|
|
|
s.read(1)
|
|
|
|
assert s.first_byte_timestamp
|
|
|
|
expected = s.first_byte_timestamp
|
|
|
|
s.read(5)
|
|
|
|
assert s.first_byte_timestamp == expected
|
|
|
|
|
|
|
|
def test_first_byte_timestamp_updated_on_readline(self):
|
|
|
|
s = cStringIO.StringIO("foobar\nfoobar\nfoobar")
|
|
|
|
s = tcp.Reader(s)
|
|
|
|
s.readline()
|
|
|
|
assert s.first_byte_timestamp
|
|
|
|
expected = s.first_byte_timestamp
|
|
|
|
s.readline()
|
|
|
|
assert s.first_byte_timestamp == expected
|