test tcpclient.source_address, increase coverage

This commit is contained in:
Maximilian Hils 2013-12-14 00:19:24 +01:00
parent 9ea4646262
commit 0187d92ec0

View File

@ -1,4 +1,4 @@
import cStringIO, Queue, time, socket import cStringIO, Queue, time, socket, random
from netlib import tcp, certutils, test from netlib import tcp, certutils, test
import mock import mock
import tutils import tutils
@ -24,6 +24,12 @@ class EchoHandler(tcp.BaseHandler):
self.wfile.flush() self.wfile.flush()
class ClientPeernameHandler(tcp.BaseHandler):
def handle(self):
self.wfile.write(str(self.connection.getpeername()))
self.wfile.flush()
class CertHandler(tcp.BaseHandler): class CertHandler(tcp.BaseHandler):
sni = None sni = None
def handle_sni(self, connection): def handle_sni(self, connection):
@ -74,6 +80,22 @@ class TestServer(test.ServerTestBase):
assert c.rfile.readline() == testval assert c.rfile.readline() == testval
class TestServerBind(test.ServerTestBase):
handler = ClientPeernameHandler
def test_bind(self):
""" Test to bind to a given random port. Try again if the random port turned out to be blocked. """
for i in range(20):
random_port = random.randrange(1024, 65535)
try:
c = tcp.TCPClient("127.0.0.1", self.port, source_address=("127.0.0.1", random_port))
c.connect()
assert c.rfile.readline() == str(("127.0.0.1", random_port))
return
except tcp.NetLibError: # port probably already in use
pass
class TestServerIPv6(test.ServerTestBase): class TestServerIPv6(test.ServerTestBase):
handler = EchoHandler handler = EchoHandler
use_ipv6 = True use_ipv6 = True