From 0187d92ec0fb3924a66b6b607f3fc50a5b311259 Mon Sep 17 00:00:00 2001 From: Maximilian Hils Date: Sat, 14 Dec 2013 00:19:24 +0100 Subject: [PATCH] test tcpclient.source_address, increase coverage --- test/test_tcp.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/test_tcp.py b/test/test_tcp.py index 75dcad13c..a4e66516e 100644 --- a/test/test_tcp.py +++ b/test/test_tcp.py @@ -1,4 +1,4 @@ -import cStringIO, Queue, time, socket +import cStringIO, Queue, time, socket, random from netlib import tcp, certutils, test import mock import tutils @@ -24,6 +24,12 @@ class EchoHandler(tcp.BaseHandler): self.wfile.flush() +class ClientPeernameHandler(tcp.BaseHandler): + def handle(self): + self.wfile.write(str(self.connection.getpeername())) + self.wfile.flush() + + class CertHandler(tcp.BaseHandler): sni = None def handle_sni(self, connection): @@ -74,6 +80,22 @@ class TestServer(test.ServerTestBase): 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): handler = EchoHandler use_ipv6 = True