diff options
author | Trent Nelson <trent.nelson@snakebite.org> | 2008-04-08 23:47:30 (GMT) |
---|---|---|
committer | Trent Nelson <trent.nelson@snakebite.org> | 2008-04-08 23:47:30 (GMT) |
commit | e41b0061dd908251a393f77b030c9e7308ecd5c5 (patch) | |
tree | 8aca119cf41a067d9b1b3b6d191ba65500a50caf /Lib/test/test_socket_ssl.py | |
parent | 02f33b43dcf9ac307449263230493348d93d2399 (diff) | |
download | cpython-e41b0061dd908251a393f77b030c9e7308ecd5c5.zip cpython-e41b0061dd908251a393f77b030c9e7308ecd5c5.tar.gz cpython-e41b0061dd908251a393f77b030c9e7308ecd5c5.tar.bz2 |
- Issue #2550: The approach used by client/server code for obtaining ports
to listen on in network-oriented tests has been refined in an effort to
facilitate running multiple instances of the entire regression test suite
in parallel without issue. test_support.bind_port() has been fixed such
that it will always return a unique port -- which wasn't always the case
with the previous implementation, especially if socket options had been
set that affected address reuse (i.e. SO_REUSEADDR, SO_REUSEPORT). The
new implementation of bind_port() will actually raise an exception if it
is passed an AF_INET/SOCK_STREAM socket with either the SO_REUSEADDR or
SO_REUSEPORT socket option set. Furthermore, if available, bind_port()
will set the SO_EXCLUSIVEADDRUSE option on the socket it's been passed.
This currently only applies to Windows. This option prevents any other
sockets from binding to the host/port we've bound to, thus removing the
possibility of the 'non-deterministic' behaviour, as Microsoft puts it,
that occurs when a second SOCK_STREAM socket binds and accepts to a
host/port that's already been bound by another socket. The optional
preferred port parameter to bind_port() has been removed. Under no
circumstances should tests be hard coding ports!
test_support.find_unused_port() has also been introduced, which will pass
a temporary socket object to bind_port() in order to obtain an unused port.
The temporary socket object is then closed and deleted, and the port is
returned. This method should only be used for obtaining an unused port
in order to pass to an external program (i.e. the -accept [port] argument
to openssl's s_server mode) or as a parameter to a server-oriented class
that doesn't give you direct access to the underlying socket used.
Finally, test_support.HOST has been introduced, which should be used for
the host argument of any relevant socket calls (i.e. bind and connect).
The following tests were updated to following the new conventions:
test_socket, test_smtplib, test_asyncore, test_ssl, test_httplib,
test_poplib, test_ftplib, test_telnetlib, test_socketserver,
test_asynchat and test_socket_ssl.
It is now possible for multiple instances of the regression test suite to
run in parallel without issue.
Diffstat (limited to 'Lib/test/test_socket_ssl.py')
-rw-r--r-- | Lib/test/test_socket_ssl.py | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/Lib/test/test_socket_ssl.py b/Lib/test/test_socket_ssl.py index 266760f..5c49427 100644 --- a/Lib/test/test_socket_ssl.py +++ b/Lib/test/test_socket_ssl.py @@ -20,6 +20,7 @@ warnings.filterwarnings( # Optionally test SSL support, if we have it in the tested platform skip_expected = not hasattr(socket, "ssl") +HOST = test_support.HOST class ConnectedTests(unittest.TestCase): @@ -86,19 +87,16 @@ class ConnectedTests(unittest.TestCase): class BasicTests(unittest.TestCase): def testRudeShutdown(self): - # Some random port to connect to. - PORT = [9934] - listener_ready = threading.Event() listener_gone = threading.Event() - - # `listener` runs in a thread. It opens a socket listening on - # PORT, and sits in an accept() until the main thread connects. - # Then it rudely closes the socket, and sets Event `listener_gone` - # to let the main thread know the socket is gone. - def listener(): - s = socket.socket() - PORT[0] = test_support.bind_port(s, '', PORT[0]) + sock = socket.socket() + port = test_support.bind_port(sock) + + # `listener` runs in a thread. It opens a socket and sits in accept() + # until the main thread connects. Then it rudely closes the socket, + # and sets Event `listener_gone` to let the main thread know the socket + # is gone. + def listener(s): s.listen(5) listener_ready.set() s.accept() @@ -108,7 +106,7 @@ class BasicTests(unittest.TestCase): def connector(): listener_ready.wait() s = socket.socket() - s.connect(('localhost', PORT[0])) + s.connect((HOST, port)) listener_gone.wait() try: ssl_sock = socket.ssl(s) @@ -118,7 +116,7 @@ class BasicTests(unittest.TestCase): raise test_support.TestFailed( 'connecting to closed SSL socket should have failed') - t = threading.Thread(target=listener) + t = threading.Thread(target=listener, args=(sock,)) t.start() connector() t.join() @@ -169,7 +167,7 @@ class OpenSSLTests(unittest.TestCase): def testBasic(self): s = socket.socket() - s.connect(("localhost", 4433)) + s.connect((HOST, OpenSSLServer.PORT)) ss = socket.ssl(s) ss.write("Foo\n") i = ss.read(4) @@ -183,7 +181,7 @@ class OpenSSLTests(unittest.TestCase): info = "/C=PT/ST=Queensland/L=Lisboa/O=Neuronio, Lda./OU=Desenvolvimento/CN=brutus.neuronio.pt/emailAddress=sampo@iki.fi" s = socket.socket() - s.connect(("localhost", 4433)) + s.connect((HOST, OpenSSLServer.PORT)) ss = socket.ssl(s) cert = ss.server() self.assertEqual(cert, info) @@ -193,6 +191,7 @@ class OpenSSLTests(unittest.TestCase): class OpenSSLServer(threading.Thread): + PORT = None def __init__(self): self.s = None self.keepServing = True @@ -211,7 +210,11 @@ class OpenSSLServer(threading.Thread): raise ValueError("No key file found! (tried %r)" % key_file) try: - cmd = "openssl s_server -cert %s -key %s -quiet" % (cert_file, key_file) + # XXX TODO: on Windows, this should make more effort to use the + # openssl.exe that would have been built by the pcbuild.sln. + self.PORT = test_support.find_unused_port() + args = (self.PORT, cert_file, key_file) + cmd = "openssl s_server -accept %d -cert %s -key %s -quiet" % args self.s = subprocess.Popen(cmd.split(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) @@ -222,7 +225,7 @@ class OpenSSLServer(threading.Thread): # let's try if it is actually up try: s = socket.socket() - s.connect(("localhost", 4433)) + s.connect((HOST, self.PORT)) s.close() if self.s.stdout.readline() != "ERROR\n": raise ValueError |