2007-08-25 12:08:43 -03:00
|
|
|
# Test the support for SSL and sockets
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
from test import test_support
|
2008-06-28 19:19:33 -03:00
|
|
|
import asyncore
|
2007-08-25 12:08:43 -03:00
|
|
|
import socket
|
2008-06-28 19:19:33 -03:00
|
|
|
import select
|
2007-08-25 12:08:43 -03:00
|
|
|
import time
|
2010-04-23 20:25:45 -03:00
|
|
|
import gc
|
2007-08-25 12:08:43 -03:00
|
|
|
import os
|
2010-04-23 20:25:45 -03:00
|
|
|
import errno
|
2007-08-25 12:08:43 -03:00
|
|
|
import pprint
|
2007-09-16 19:06:00 -03:00
|
|
|
import urllib, urlparse
|
2007-08-25 12:08:43 -03:00
|
|
|
import traceback
|
2010-04-23 19:54:59 -03:00
|
|
|
import weakref
|
2010-08-04 14:38:33 -03:00
|
|
|
import functools
|
|
|
|
import platform
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2007-09-16 19:06:00 -03:00
|
|
|
from BaseHTTPServer import HTTPServer
|
|
|
|
from SimpleHTTPServer import SimpleHTTPRequestHandler
|
|
|
|
|
2011-05-22 08:22:28 -03:00
|
|
|
ssl = test_support.import_module("ssl")
|
2007-08-25 12:08:43 -03:00
|
|
|
|
- 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.
2008-04-08 20:47:30 -03:00
|
|
|
HOST = test_support.HOST
|
2007-08-25 12:08:43 -03:00
|
|
|
CERTFILE = None
|
2007-09-16 19:06:00 -03:00
|
|
|
SVN_PYTHON_ORG_ROOT_CERT = None
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2007-08-26 22:03:18 -03:00
|
|
|
def handle_error(prefix):
|
|
|
|
exc_format = ' '.join(traceback.format_exception(*sys.exc_info()))
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(prefix + exc_format)
|
2007-08-26 22:03:18 -03:00
|
|
|
|
2010-04-27 06:51:18 -03:00
|
|
|
|
|
|
|
class BasicTests(unittest.TestCase):
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_sslwrap_simple(self):
|
2010-04-27 06:51:18 -03:00
|
|
|
# A crude test for the legacy API
|
2008-09-29 15:56:38 -03:00
|
|
|
try:
|
|
|
|
ssl.sslwrap_simple(socket.socket(socket.AF_INET))
|
|
|
|
except IOError, e:
|
|
|
|
if e.errno == 32: # broken pipe when ssl_sock.do_handshake(), this test doesn't care about that
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
try:
|
|
|
|
ssl.sslwrap_simple(socket.socket(socket.AF_INET)._sock)
|
|
|
|
except IOError, e:
|
|
|
|
if e.errno == 32: # broken pipe when ssl_sock.do_handshake(), this test doesn't care about that
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
2007-08-26 22:03:18 -03:00
|
|
|
|
2010-08-04 14:38:33 -03:00
|
|
|
# Issue #9415: Ubuntu hijacks their OpenSSL and forcefully disables SSLv2
|
|
|
|
def skip_if_broken_ubuntu_ssl(func):
|
2011-05-09 20:52:03 -03:00
|
|
|
if hasattr(ssl, 'PROTOCOL_SSLv2'):
|
|
|
|
# We need to access the lower-level wrapper in order to create an
|
|
|
|
# implicit SSL context without trying to connect or listen.
|
2010-08-04 14:38:33 -03:00
|
|
|
try:
|
2011-05-09 20:52:03 -03:00
|
|
|
import _ssl
|
|
|
|
except ImportError:
|
|
|
|
# The returned function won't get executed, just ignore the error
|
|
|
|
pass
|
|
|
|
@functools.wraps(func)
|
|
|
|
def f(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
s = socket.socket(socket.AF_INET)
|
|
|
|
_ssl.sslwrap(s._sock, 0, None, None,
|
|
|
|
ssl.CERT_NONE, ssl.PROTOCOL_SSLv2, None, None)
|
|
|
|
except ssl.SSLError as e:
|
|
|
|
if (ssl.OPENSSL_VERSION_INFO == (0, 9, 8, 15, 15) and
|
|
|
|
platform.linux_distribution() == ('debian', 'squeeze/sid', '')
|
|
|
|
and 'Invalid SSL protocol variant specified' in str(e)):
|
|
|
|
raise unittest.SkipTest("Patched Ubuntu OpenSSL breaks behaviour")
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return f
|
|
|
|
else:
|
|
|
|
return func
|
2010-08-04 14:38:33 -03:00
|
|
|
|
|
|
|
|
|
|
|
class BasicSocketTests(unittest.TestCase):
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_constants(self):
|
2011-05-09 20:52:03 -03:00
|
|
|
#ssl.PROTOCOL_SSLv2
|
2007-09-10 18:51:02 -03:00
|
|
|
ssl.PROTOCOL_SSLv23
|
|
|
|
ssl.PROTOCOL_SSLv3
|
|
|
|
ssl.PROTOCOL_TLSv1
|
|
|
|
ssl.CERT_NONE
|
|
|
|
ssl.CERT_OPTIONAL
|
|
|
|
ssl.CERT_REQUIRED
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_random(self):
|
2007-09-10 18:51:02 -03:00
|
|
|
v = ssl.RAND_status()
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n RAND_status is %d (%s)\n"
|
|
|
|
% (v, (v and "sufficient randomness") or
|
|
|
|
"insufficient randomness"))
|
2007-08-26 16:35:09 -03:00
|
|
|
try:
|
2007-09-10 18:51:02 -03:00
|
|
|
ssl.RAND_egd(1)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
2007-08-26 16:35:09 -03:00
|
|
|
else:
|
2007-09-10 18:51:02 -03:00
|
|
|
print "didn't raise TypeError"
|
|
|
|
ssl.RAND_add("this is a random string", 75.0)
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_parse_cert(self):
|
2007-09-10 18:51:02 -03:00
|
|
|
# note that this uses an 'unofficial' function in _ssl.c,
|
|
|
|
# provided solely for this test, to exercise the certificate
|
|
|
|
# parsing code
|
|
|
|
p = ssl._ssl._test_decode_cert(CERTFILE, False)
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n" + pprint.pformat(p) + "\n")
|
2011-10-01 14:30:58 -03:00
|
|
|
self.assertEqual(p['subject'],
|
|
|
|
((('countryName', u'US'),),
|
|
|
|
(('stateOrProvinceName', u'Delaware'),),
|
|
|
|
(('localityName', u'Wilmington'),),
|
|
|
|
(('organizationName', u'Python Software Foundation'),),
|
|
|
|
(('organizationalUnitName', u'SSL'),),
|
|
|
|
(('commonName', u'somemachine.python.org'),)),
|
|
|
|
)
|
|
|
|
# Issue #13034: the subjectAltName in some certificates
|
|
|
|
# (notably projects.developer.nokia.com:443) wasn't parsed
|
|
|
|
p = ssl._ssl._test_decode_cert(NOKIACERT)
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n" + pprint.pformat(p) + "\n")
|
|
|
|
self.assertEqual(p['subjectAltName'],
|
|
|
|
(('DNS', 'projects.developer.nokia.com'),
|
|
|
|
('DNS', 'projects.forum.nokia.com'))
|
|
|
|
)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_DER_to_PEM(self):
|
|
|
|
with open(SVN_PYTHON_ORG_ROOT_CERT, 'r') as f:
|
|
|
|
pem = f.read()
|
2007-09-16 19:06:00 -03:00
|
|
|
d1 = ssl.PEM_cert_to_DER_cert(pem)
|
|
|
|
p2 = ssl.DER_cert_to_PEM_cert(d1)
|
|
|
|
d2 = ssl.PEM_cert_to_DER_cert(p2)
|
2010-04-27 07:32:58 -03:00
|
|
|
self.assertEqual(d1, d2)
|
2010-04-27 19:03:37 -03:00
|
|
|
if not p2.startswith(ssl.PEM_HEADER + '\n'):
|
|
|
|
self.fail("DER-to-PEM didn't include correct header:\n%r\n" % p2)
|
|
|
|
if not p2.endswith('\n' + ssl.PEM_FOOTER + '\n'):
|
|
|
|
self.fail("DER-to-PEM didn't include correct footer:\n%r\n" % p2)
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2010-04-05 18:35:07 -03:00
|
|
|
def test_openssl_version(self):
|
|
|
|
n = ssl.OPENSSL_VERSION_NUMBER
|
|
|
|
t = ssl.OPENSSL_VERSION_INFO
|
|
|
|
s = ssl.OPENSSL_VERSION
|
|
|
|
self.assertIsInstance(n, (int, long))
|
|
|
|
self.assertIsInstance(t, tuple)
|
|
|
|
self.assertIsInstance(s, str)
|
|
|
|
# Some sanity checks follow
|
|
|
|
# >= 0.9
|
|
|
|
self.assertGreaterEqual(n, 0x900000)
|
|
|
|
# < 2.0
|
|
|
|
self.assertLess(n, 0x20000000)
|
|
|
|
major, minor, fix, patch, status = t
|
|
|
|
self.assertGreaterEqual(major, 0)
|
|
|
|
self.assertLess(major, 2)
|
|
|
|
self.assertGreaterEqual(minor, 0)
|
|
|
|
self.assertLess(minor, 256)
|
|
|
|
self.assertGreaterEqual(fix, 0)
|
|
|
|
self.assertLess(fix, 256)
|
|
|
|
self.assertGreaterEqual(patch, 0)
|
|
|
|
self.assertLessEqual(patch, 26)
|
|
|
|
self.assertGreaterEqual(status, 0)
|
|
|
|
self.assertLessEqual(status, 15)
|
|
|
|
# Version string as returned by OpenSSL, the format might change
|
|
|
|
self.assertTrue(s.startswith("OpenSSL {:d}.{:d}.{:d}".format(major, minor, fix)),
|
|
|
|
(s, t))
|
|
|
|
|
2010-04-17 14:10:38 -03:00
|
|
|
def test_ciphers(self):
|
|
|
|
if not test_support.is_resource_enabled('network'):
|
|
|
|
return
|
|
|
|
remote = ("svn.python.org", 443)
|
2010-10-31 10:26:53 -03:00
|
|
|
with test_support.transient_internet(remote[0]):
|
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_NONE, ciphers="ALL")
|
2010-04-17 14:10:38 -03:00
|
|
|
s.connect(remote)
|
2010-10-31 10:26:53 -03:00
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_NONE, ciphers="DEFAULT")
|
|
|
|
s.connect(remote)
|
|
|
|
# Error checking occurs when connecting, because the SSL context
|
|
|
|
# isn't created before.
|
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_NONE, ciphers="^$:,;?*'dorothyx")
|
|
|
|
with self.assertRaisesRegexp(ssl.SSLError, "No cipher can be selected"):
|
|
|
|
s.connect(remote)
|
2010-04-17 14:10:38 -03:00
|
|
|
|
2010-04-23 19:54:59 -03:00
|
|
|
@test_support.cpython_only
|
|
|
|
def test_refcycle(self):
|
|
|
|
# Issue #7943: an SSL object doesn't create reference cycles with
|
|
|
|
# itself.
|
|
|
|
s = socket.socket(socket.AF_INET)
|
|
|
|
ss = ssl.wrap_socket(s)
|
|
|
|
wr = weakref.ref(ss)
|
|
|
|
del ss
|
|
|
|
self.assertEqual(wr(), None)
|
|
|
|
|
2010-09-14 11:37:18 -03:00
|
|
|
def test_wrapped_unconnected(self):
|
|
|
|
# The _delegate_methods in socket.py are correctly delegated to by an
|
|
|
|
# unconnected SSLSocket, so they will raise a socket.error rather than
|
|
|
|
# something unexpected like TypeError.
|
|
|
|
s = socket.socket(socket.AF_INET)
|
|
|
|
ss = ssl.wrap_socket(s)
|
|
|
|
self.assertRaises(socket.error, ss.recv, 1)
|
|
|
|
self.assertRaises(socket.error, ss.recv_into, bytearray(b'x'))
|
|
|
|
self.assertRaises(socket.error, ss.recvfrom, 1)
|
|
|
|
self.assertRaises(socket.error, ss.recvfrom_into, bytearray(b'x'), 1)
|
|
|
|
self.assertRaises(socket.error, ss.send, b'x')
|
|
|
|
self.assertRaises(socket.error, ss.sendto, b'x', ('0.0.0.0', 0))
|
|
|
|
|
2010-04-05 18:35:07 -03:00
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
class NetworkedTests(unittest.TestCase):
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_connect(self):
|
2010-09-09 10:35:44 -03:00
|
|
|
with test_support.transient_internet("svn.python.org"):
|
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_NONE)
|
2007-09-16 19:06:00 -03:00
|
|
|
s.connect(("svn.python.org", 443))
|
2010-09-09 10:35:44 -03:00
|
|
|
c = s.getpeercert()
|
|
|
|
if c:
|
|
|
|
self.fail("Peer cert %s shouldn't be here!")
|
2007-09-16 19:06:00 -03:00
|
|
|
s.close()
|
|
|
|
|
2010-09-09 10:35:44 -03:00
|
|
|
# this should fail because we have no verification certs
|
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED)
|
|
|
|
try:
|
|
|
|
s.connect(("svn.python.org", 443))
|
|
|
|
except ssl.SSLError:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
# this should succeed because we specify the root cert
|
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED,
|
|
|
|
ca_certs=SVN_PYTHON_ORG_ROOT_CERT)
|
|
|
|
try:
|
|
|
|
s.connect(("svn.python.org", 443))
|
|
|
|
finally:
|
|
|
|
s.close()
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2011-02-26 19:35:27 -04:00
|
|
|
def test_connect_ex(self):
|
|
|
|
# Issue #11326: check connect_ex() implementation
|
|
|
|
with test_support.transient_internet("svn.python.org"):
|
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED,
|
|
|
|
ca_certs=SVN_PYTHON_ORG_ROOT_CERT)
|
|
|
|
try:
|
|
|
|
self.assertEqual(0, s.connect_ex(("svn.python.org", 443)))
|
|
|
|
self.assertTrue(s.getpeercert())
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
def test_non_blocking_connect_ex(self):
|
|
|
|
# Issue #11326: non-blocking connect_ex() should allow handshake
|
|
|
|
# to proceed after the socket gets ready.
|
|
|
|
with test_support.transient_internet("svn.python.org"):
|
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED,
|
|
|
|
ca_certs=SVN_PYTHON_ORG_ROOT_CERT,
|
|
|
|
do_handshake_on_connect=False)
|
|
|
|
try:
|
|
|
|
s.setblocking(False)
|
|
|
|
rc = s.connect_ex(('svn.python.org', 443))
|
2011-02-27 11:45:22 -04:00
|
|
|
# EWOULDBLOCK under Windows, EINPROGRESS elsewhere
|
|
|
|
self.assertIn(rc, (0, errno.EINPROGRESS, errno.EWOULDBLOCK))
|
2011-02-26 19:35:27 -04:00
|
|
|
# Wait for connect to finish
|
|
|
|
select.select([], [s], [], 5.0)
|
|
|
|
# Non-blocking handshake
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
s.do_handshake()
|
|
|
|
break
|
|
|
|
except ssl.SSLError as err:
|
|
|
|
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
|
|
|
|
select.select([s], [], [], 5.0)
|
|
|
|
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
|
|
|
|
select.select([], [s], [], 5.0)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
# SSL established
|
|
|
|
self.assertTrue(s.getpeercert())
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
2010-04-24 07:43:57 -03:00
|
|
|
@unittest.skipIf(os.name == "nt", "Can't use a socket as a file under Windows")
|
|
|
|
def test_makefile_close(self):
|
|
|
|
# Issue #5238: creating a file-like object with makefile() shouldn't
|
|
|
|
# delay closing the underlying "real socket" (here tested with its
|
|
|
|
# file descriptor, hence skipping the test under Windows).
|
2010-09-09 10:35:44 -03:00
|
|
|
with test_support.transient_internet("svn.python.org"):
|
|
|
|
ss = ssl.wrap_socket(socket.socket(socket.AF_INET))
|
|
|
|
ss.connect(("svn.python.org", 443))
|
|
|
|
fd = ss.fileno()
|
|
|
|
f = ss.makefile()
|
|
|
|
f.close()
|
|
|
|
# The fd is still open
|
2010-04-24 07:43:57 -03:00
|
|
|
os.read(fd, 0)
|
2010-09-09 10:35:44 -03:00
|
|
|
# Closing the SSL socket should close the fd too
|
|
|
|
ss.close()
|
|
|
|
gc.collect()
|
|
|
|
with self.assertRaises(OSError) as e:
|
|
|
|
os.read(fd, 0)
|
|
|
|
self.assertEqual(e.exception.errno, errno.EBADF)
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_non_blocking_handshake(self):
|
2010-09-09 10:35:44 -03:00
|
|
|
with test_support.transient_internet("svn.python.org"):
|
|
|
|
s = socket.socket(socket.AF_INET)
|
|
|
|
s.connect(("svn.python.org", 443))
|
|
|
|
s.setblocking(False)
|
|
|
|
s = ssl.wrap_socket(s,
|
|
|
|
cert_reqs=ssl.CERT_NONE,
|
|
|
|
do_handshake_on_connect=False)
|
|
|
|
count = 0
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
count += 1
|
|
|
|
s.do_handshake()
|
|
|
|
break
|
|
|
|
except ssl.SSLError, err:
|
|
|
|
if err.args[0] == ssl.SSL_ERROR_WANT_READ:
|
|
|
|
select.select([s], [], [])
|
|
|
|
elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
|
|
|
|
select.select([], [s], [])
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
s.close()
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\nNeeded %d calls to do_handshake() to establish session.\n" % count)
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_get_server_certificate(self):
|
2010-09-09 10:35:44 -03:00
|
|
|
with test_support.transient_internet("svn.python.org"):
|
|
|
|
pem = ssl.get_server_certificate(("svn.python.org", 443))
|
|
|
|
if not pem:
|
|
|
|
self.fail("No server certificate on svn.python.org:443!")
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2010-09-09 10:35:44 -03:00
|
|
|
try:
|
|
|
|
pem = ssl.get_server_certificate(("svn.python.org", 443), ca_certs=CERTFILE)
|
|
|
|
except ssl.SSLError:
|
|
|
|
#should fail
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.fail("Got server certificate %s for svn.python.org!" % pem)
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2010-09-09 10:35:44 -03:00
|
|
|
pem = ssl.get_server_certificate(("svn.python.org", 443), ca_certs=SVN_PYTHON_ORG_ROOT_CERT)
|
|
|
|
if not pem:
|
|
|
|
self.fail("No server certificate on svn.python.org:443!")
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\nVerified certificate for svn.python.org:443 is\n%s\n" % pem)
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2010-04-21 16:28:03 -03:00
|
|
|
def test_algorithms(self):
|
|
|
|
# Issue #8484: all algorithms should be available when verifying a
|
|
|
|
# certificate.
|
2010-04-22 15:00:41 -03:00
|
|
|
# SHA256 was added in OpenSSL 0.9.8
|
|
|
|
if ssl.OPENSSL_VERSION_INFO < (0, 9, 8, 0, 15):
|
|
|
|
self.skipTest("SHA256 not available on %r" % ssl.OPENSSL_VERSION)
|
2010-04-21 16:28:03 -03:00
|
|
|
# NOTE: https://sha256.tbs-internet.com is another possible test host
|
2011-01-08 06:32:51 -04:00
|
|
|
remote = ("sha256.tbs-internet.com", 443)
|
2010-04-21 16:28:03 -03:00
|
|
|
sha256_cert = os.path.join(os.path.dirname(__file__), "sha256.pem")
|
2011-01-08 06:32:51 -04:00
|
|
|
with test_support.transient_internet("sha256.tbs-internet.com"):
|
2010-09-07 18:40:25 -03:00
|
|
|
s = ssl.wrap_socket(socket.socket(socket.AF_INET),
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED,
|
|
|
|
ca_certs=sha256_cert,)
|
2010-04-21 16:28:03 -03:00
|
|
|
try:
|
|
|
|
s.connect(remote)
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\nCipher with %r is %r\n" %
|
|
|
|
(remote, s.cipher()))
|
|
|
|
sys.stdout.write("Certificate is:\n%s\n" %
|
|
|
|
pprint.pformat(s.getpeercert()))
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
try:
|
|
|
|
import threading
|
|
|
|
except ImportError:
|
|
|
|
_have_threads = False
|
|
|
|
else:
|
|
|
|
_have_threads = True
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
class ThreadedEchoServer(threading.Thread):
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
class ConnectionHandler(threading.Thread):
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
"""A mildly complicated class, because we want it to work both
|
|
|
|
with and without the SSL wrapper around the socket connection, so
|
|
|
|
that we can test the STARTTLS functionality."""
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
def __init__(self, server, connsock):
|
|
|
|
self.server = server
|
2007-08-26 16:35:09 -03:00
|
|
|
self.running = False
|
2007-09-10 18:51:02 -03:00
|
|
|
self.sock = connsock
|
|
|
|
self.sock.setblocking(1)
|
|
|
|
self.sslconn = None
|
|
|
|
threading.Thread.__init__(self)
|
2008-08-18 15:39:57 -03:00
|
|
|
self.daemon = True
|
2007-08-26 16:35:09 -03:00
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
def show_conn_details(self):
|
|
|
|
if self.server.certreqs == ssl.CERT_REQUIRED:
|
|
|
|
cert = self.sslconn.getpeercert()
|
|
|
|
if test_support.verbose and self.server.chatty:
|
|
|
|
sys.stdout.write(" client cert is " + pprint.pformat(cert) + "\n")
|
|
|
|
cert_binary = self.sslconn.getpeercert(True)
|
|
|
|
if test_support.verbose and self.server.chatty:
|
|
|
|
sys.stdout.write(" cert binary is " + str(len(cert_binary)) + " bytes\n")
|
|
|
|
cipher = self.sslconn.cipher()
|
|
|
|
if test_support.verbose and self.server.chatty:
|
|
|
|
sys.stdout.write(" server: connection cipher is now " + str(cipher) + "\n")
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def wrap_conn(self):
|
2007-08-25 12:08:43 -03:00
|
|
|
try:
|
2007-09-10 18:51:02 -03:00
|
|
|
self.sslconn = ssl.wrap_socket(self.sock, server_side=True,
|
|
|
|
certfile=self.server.certificate,
|
|
|
|
ssl_version=self.server.protocol,
|
|
|
|
ca_certs=self.server.cacerts,
|
2010-04-17 14:10:38 -03:00
|
|
|
cert_reqs=self.server.certreqs,
|
|
|
|
ciphers=self.server.ciphers)
|
2010-04-27 07:32:58 -03:00
|
|
|
except ssl.SSLError:
|
|
|
|
# XXX Various errors can have happened here, for example
|
|
|
|
# a mismatching protocol version, an invalid certificate,
|
|
|
|
# or a low-level bug. This should be made more discriminating.
|
2007-09-10 18:51:02 -03:00
|
|
|
if self.server.chatty:
|
|
|
|
handle_error("\n server: bad connection attempt from " +
|
|
|
|
str(self.sock.getpeername()) + ":\n")
|
2008-06-28 19:19:33 -03:00
|
|
|
self.close()
|
2010-04-27 07:32:58 -03:00
|
|
|
self.running = False
|
|
|
|
self.server.stop()
|
2007-09-10 18:51:02 -03:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
if self.sslconn:
|
|
|
|
return self.sslconn.read()
|
|
|
|
else:
|
|
|
|
return self.sock.recv(1024)
|
|
|
|
|
|
|
|
def write(self, bytes):
|
|
|
|
if self.sslconn:
|
|
|
|
return self.sslconn.write(bytes)
|
|
|
|
else:
|
|
|
|
return self.sock.send(bytes)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
if self.sslconn:
|
|
|
|
self.sslconn.close()
|
|
|
|
else:
|
2008-06-28 19:19:33 -03:00
|
|
|
self.sock._sock.close()
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def run(self):
|
2007-09-10 18:51:02 -03:00
|
|
|
self.running = True
|
|
|
|
if not self.server.starttls_server:
|
2008-06-28 19:19:33 -03:00
|
|
|
if isinstance(self.sock, ssl.SSLSocket):
|
|
|
|
self.sslconn = self.sock
|
|
|
|
elif not self.wrap_conn():
|
2007-09-10 18:51:02 -03:00
|
|
|
return
|
2008-06-28 19:19:33 -03:00
|
|
|
self.show_conn_details()
|
2007-09-10 18:51:02 -03:00
|
|
|
while self.running:
|
|
|
|
try:
|
|
|
|
msg = self.read()
|
|
|
|
if not msg:
|
|
|
|
# eof, so quit this handler
|
|
|
|
self.running = False
|
|
|
|
self.close()
|
|
|
|
elif msg.strip() == 'over':
|
|
|
|
if test_support.verbose and self.server.connectionchatty:
|
|
|
|
sys.stdout.write(" server: client closed connection\n")
|
|
|
|
self.close()
|
|
|
|
return
|
|
|
|
elif self.server.starttls_server and msg.strip() == 'STARTTLS':
|
|
|
|
if test_support.verbose and self.server.connectionchatty:
|
|
|
|
sys.stdout.write(" server: read STARTTLS from client, sending OK...\n")
|
|
|
|
self.write("OK\n")
|
|
|
|
if not self.wrap_conn():
|
|
|
|
return
|
2008-08-12 13:31:21 -03:00
|
|
|
elif self.server.starttls_server and self.sslconn and msg.strip() == 'ENDTLS':
|
|
|
|
if test_support.verbose and self.server.connectionchatty:
|
|
|
|
sys.stdout.write(" server: read ENDTLS from client, sending OK...\n")
|
|
|
|
self.write("OK\n")
|
|
|
|
self.sslconn.unwrap()
|
|
|
|
self.sslconn = None
|
|
|
|
if test_support.verbose and self.server.connectionchatty:
|
|
|
|
sys.stdout.write(" server: connection is now unencrypted...\n")
|
2007-09-10 18:51:02 -03:00
|
|
|
else:
|
|
|
|
if (test_support.verbose and
|
|
|
|
self.server.connectionchatty):
|
|
|
|
ctype = (self.sslconn and "encrypted") or "unencrypted"
|
|
|
|
sys.stdout.write(" server: read %s (%s), sending back %s (%s)...\n"
|
|
|
|
% (repr(msg), ctype, repr(msg.lower()), ctype))
|
|
|
|
self.write(msg.lower())
|
|
|
|
except ssl.SSLError:
|
|
|
|
if self.server.chatty:
|
|
|
|
handle_error("Test server failure:\n")
|
|
|
|
self.close()
|
2007-08-25 12:08:43 -03:00
|
|
|
self.running = False
|
2007-09-10 18:51:02 -03:00
|
|
|
# normally, we'd just stop here, but for the test
|
|
|
|
# harness, we want to stop the server
|
|
|
|
self.server.stop()
|
|
|
|
|
- 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.
2008-04-08 20:47:30 -03:00
|
|
|
def __init__(self, certificate, ssl_version=None,
|
2010-04-27 07:32:58 -03:00
|
|
|
certreqs=None, cacerts=None,
|
2008-06-28 19:19:33 -03:00
|
|
|
chatty=True, connectionchatty=False, starttls_server=False,
|
2010-04-17 14:10:38 -03:00
|
|
|
wrap_accepting_socket=False, ciphers=None):
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
if ssl_version is None:
|
|
|
|
ssl_version = ssl.PROTOCOL_TLSv1
|
|
|
|
if certreqs is None:
|
|
|
|
certreqs = ssl.CERT_NONE
|
|
|
|
self.certificate = certificate
|
|
|
|
self.protocol = ssl_version
|
|
|
|
self.certreqs = certreqs
|
|
|
|
self.cacerts = cacerts
|
2010-04-17 14:10:38 -03:00
|
|
|
self.ciphers = ciphers
|
2007-09-10 18:51:02 -03:00
|
|
|
self.chatty = chatty
|
|
|
|
self.connectionchatty = connectionchatty
|
|
|
|
self.starttls_server = starttls_server
|
|
|
|
self.sock = socket.socket()
|
|
|
|
self.flag = None
|
2008-06-28 19:19:33 -03:00
|
|
|
if wrap_accepting_socket:
|
|
|
|
self.sock = ssl.wrap_socket(self.sock, server_side=True,
|
|
|
|
certfile=self.certificate,
|
|
|
|
cert_reqs = self.certreqs,
|
|
|
|
ca_certs = self.cacerts,
|
2010-04-17 14:10:38 -03:00
|
|
|
ssl_version = self.protocol,
|
|
|
|
ciphers = self.ciphers)
|
2008-06-28 19:19:33 -03:00
|
|
|
if test_support.verbose and self.chatty:
|
|
|
|
sys.stdout.write(' server: wrapped server socket as %s\n' % str(self.sock))
|
|
|
|
self.port = test_support.bind_port(self.sock)
|
2007-09-10 18:51:02 -03:00
|
|
|
self.active = False
|
|
|
|
threading.Thread.__init__(self)
|
2008-08-18 15:39:57 -03:00
|
|
|
self.daemon = True
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def start(self, flag=None):
|
2007-09-10 18:51:02 -03:00
|
|
|
self.flag = flag
|
|
|
|
threading.Thread.start(self)
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def run(self):
|
2010-04-27 06:51:18 -03:00
|
|
|
self.sock.settimeout(0.05)
|
2007-09-10 18:51:02 -03:00
|
|
|
self.sock.listen(5)
|
|
|
|
self.active = True
|
|
|
|
if self.flag:
|
|
|
|
# signal an event
|
|
|
|
self.flag.set()
|
|
|
|
while self.active:
|
|
|
|
try:
|
|
|
|
newconn, connaddr = self.sock.accept()
|
|
|
|
if test_support.verbose and self.chatty:
|
|
|
|
sys.stdout.write(' server: new connection from '
|
|
|
|
+ str(connaddr) + '\n')
|
|
|
|
handler = self.ConnectionHandler(self, newconn)
|
|
|
|
handler.start()
|
|
|
|
except socket.timeout:
|
|
|
|
pass
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.stop()
|
2008-06-28 19:19:33 -03:00
|
|
|
self.sock.close()
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def stop(self):
|
2007-09-10 18:51:02 -03:00
|
|
|
self.active = False
|
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
class AsyncoreEchoServer(threading.Thread):
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
class EchoServer(asyncore.dispatcher):
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
class ConnectionHandler(asyncore.dispatcher_with_send):
|
2008-06-28 19:19:33 -03:00
|
|
|
|
|
|
|
def __init__(self, conn, certfile):
|
|
|
|
asyncore.dispatcher_with_send.__init__(self, conn)
|
|
|
|
self.socket = ssl.wrap_socket(conn, server_side=True,
|
|
|
|
certfile=certfile,
|
2010-04-24 17:04:58 -03:00
|
|
|
do_handshake_on_connect=False)
|
|
|
|
self._ssl_accepting = True
|
2008-06-28 19:19:33 -03:00
|
|
|
|
|
|
|
def readable(self):
|
|
|
|
if isinstance(self.socket, ssl.SSLSocket):
|
|
|
|
while self.socket.pending() > 0:
|
|
|
|
self.handle_read_event()
|
|
|
|
return True
|
|
|
|
|
2010-04-24 17:04:58 -03:00
|
|
|
def _do_ssl_handshake(self):
|
|
|
|
try:
|
|
|
|
self.socket.do_handshake()
|
|
|
|
except ssl.SSLError, err:
|
|
|
|
if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
|
|
|
|
ssl.SSL_ERROR_WANT_WRITE):
|
|
|
|
return
|
|
|
|
elif err.args[0] == ssl.SSL_ERROR_EOF:
|
|
|
|
return self.handle_close()
|
|
|
|
raise
|
|
|
|
except socket.error, err:
|
|
|
|
if err.args[0] == errno.ECONNABORTED:
|
|
|
|
return self.handle_close()
|
|
|
|
else:
|
|
|
|
self._ssl_accepting = False
|
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
def handle_read(self):
|
2010-04-24 17:04:58 -03:00
|
|
|
if self._ssl_accepting:
|
|
|
|
self._do_ssl_handshake()
|
|
|
|
else:
|
|
|
|
data = self.recv(1024)
|
2010-04-27 07:32:58 -03:00
|
|
|
if data and data.strip() != 'over':
|
|
|
|
self.send(data.lower())
|
2008-06-28 19:19:33 -03:00
|
|
|
|
|
|
|
def handle_close(self):
|
2008-06-28 20:00:39 -03:00
|
|
|
self.close()
|
2008-06-28 19:19:33 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(" server: closed connection %s\n" % self.socket)
|
|
|
|
|
|
|
|
def handle_error(self):
|
|
|
|
raise
|
|
|
|
|
|
|
|
def __init__(self, certfile):
|
|
|
|
self.certfile = certfile
|
|
|
|
asyncore.dispatcher.__init__(self)
|
|
|
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.port = test_support.bind_port(self.socket)
|
|
|
|
self.listen(5)
|
|
|
|
|
|
|
|
def handle_accept(self):
|
|
|
|
sock_obj, addr = self.accept()
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(" server: new connection from %s:%s\n" %addr)
|
|
|
|
self.ConnectionHandler(sock_obj, self.certfile)
|
|
|
|
|
|
|
|
def handle_error(self):
|
|
|
|
raise
|
|
|
|
|
|
|
|
def __init__(self, certfile):
|
|
|
|
self.flag = None
|
|
|
|
self.active = False
|
|
|
|
self.server = self.EchoServer(certfile)
|
|
|
|
self.port = self.server.port
|
|
|
|
threading.Thread.__init__(self)
|
2008-08-18 15:39:57 -03:00
|
|
|
self.daemon = True
|
2008-06-28 19:19:33 -03:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "<%s %s>" % (self.__class__.__name__, self.server)
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def start(self, flag=None):
|
2008-06-28 19:19:33 -03:00
|
|
|
self.flag = flag
|
|
|
|
threading.Thread.start(self)
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def run(self):
|
2008-06-28 19:19:33 -03:00
|
|
|
self.active = True
|
|
|
|
if self.flag:
|
|
|
|
self.flag.set()
|
|
|
|
while self.active:
|
2010-04-27 07:32:58 -03:00
|
|
|
asyncore.loop(0.05)
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def stop(self):
|
2008-06-28 19:19:33 -03:00
|
|
|
self.active = False
|
|
|
|
self.server.close()
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
class SocketServerHTTPSServer(threading.Thread):
|
2007-09-16 19:06:00 -03:00
|
|
|
|
|
|
|
class HTTPSServer(HTTPServer):
|
|
|
|
|
|
|
|
def __init__(self, server_address, RequestHandlerClass, certfile):
|
|
|
|
HTTPServer.__init__(self, server_address, RequestHandlerClass)
|
|
|
|
# we assume the certfile contains both private key and certificate
|
|
|
|
self.certfile = certfile
|
|
|
|
self.allow_reuse_address = True
|
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
def __str__(self):
|
|
|
|
return ('<%s %s:%s>' %
|
|
|
|
(self.__class__.__name__,
|
|
|
|
self.server_name,
|
|
|
|
self.server_port))
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def get_request(self):
|
2007-09-16 19:06:00 -03:00
|
|
|
# override this to wrap socket with SSL
|
|
|
|
sock, addr = self.socket.accept()
|
|
|
|
sslconn = ssl.wrap_socket(sock, server_side=True,
|
|
|
|
certfile=self.certfile)
|
|
|
|
return sslconn, addr
|
|
|
|
|
|
|
|
class RootedHTTPRequestHandler(SimpleHTTPRequestHandler):
|
|
|
|
# need to override translate_path to get a known root,
|
|
|
|
# instead of using os.curdir, since the test could be
|
|
|
|
# run from anywhere
|
|
|
|
|
|
|
|
server_version = "TestHTTPS/1.0"
|
|
|
|
|
|
|
|
root = None
|
|
|
|
|
|
|
|
def translate_path(self, path):
|
|
|
|
"""Translate a /-separated PATH to the local filename syntax.
|
|
|
|
|
|
|
|
Components that mean special things to the local file system
|
|
|
|
(e.g. drive or directory names) are ignored. (XXX They should
|
|
|
|
probably be diagnosed.)
|
|
|
|
|
|
|
|
"""
|
|
|
|
# abandon query parameters
|
|
|
|
path = urlparse.urlparse(path)[2]
|
|
|
|
path = os.path.normpath(urllib.unquote(path))
|
|
|
|
words = path.split('/')
|
|
|
|
words = filter(None, words)
|
|
|
|
path = self.root
|
|
|
|
for word in words:
|
|
|
|
drive, word = os.path.splitdrive(word)
|
|
|
|
head, word = os.path.split(word)
|
|
|
|
if word in self.root: continue
|
|
|
|
path = os.path.join(path, word)
|
|
|
|
return path
|
|
|
|
|
|
|
|
def log_message(self, format, *args):
|
|
|
|
|
|
|
|
# we override this to suppress logging unless "verbose"
|
|
|
|
|
|
|
|
if test_support.verbose:
|
2008-06-28 19:19:33 -03:00
|
|
|
sys.stdout.write(" server (%s:%d %s):\n [%s] %s\n" %
|
|
|
|
(self.server.server_address,
|
2007-09-16 19:06:00 -03:00
|
|
|
self.server.server_port,
|
|
|
|
self.request.cipher(),
|
|
|
|
self.log_date_time_string(),
|
|
|
|
format%args))
|
|
|
|
|
|
|
|
|
- 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.
2008-04-08 20:47:30 -03:00
|
|
|
def __init__(self, certfile):
|
2007-09-16 19:06:00 -03:00
|
|
|
self.flag = None
|
|
|
|
self.RootedHTTPRequestHandler.root = os.path.split(CERTFILE)[0]
|
|
|
|
self.server = self.HTTPSServer(
|
2010-04-27 05:40:51 -03:00
|
|
|
(HOST, 0), self.RootedHTTPRequestHandler, certfile)
|
|
|
|
self.port = self.server.server_port
|
2007-09-16 19:06:00 -03:00
|
|
|
threading.Thread.__init__(self)
|
2008-08-18 15:39:57 -03:00
|
|
|
self.daemon = True
|
2007-09-16 19:06:00 -03:00
|
|
|
|
|
|
|
def __str__(self):
|
2008-06-28 19:19:33 -03:00
|
|
|
return "<%s %s>" % (self.__class__.__name__, self.server)
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def start(self, flag=None):
|
2007-09-16 19:06:00 -03:00
|
|
|
self.flag = flag
|
|
|
|
threading.Thread.start(self)
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def run(self):
|
2007-09-16 19:06:00 -03:00
|
|
|
if self.flag:
|
|
|
|
self.flag.set()
|
2010-04-27 06:51:18 -03:00
|
|
|
self.server.serve_forever(0.05)
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def stop(self):
|
2010-04-27 06:51:18 -03:00
|
|
|
self.server.shutdown()
|
2007-09-16 19:06:00 -03:00
|
|
|
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def bad_cert_test(certfile):
|
|
|
|
"""
|
|
|
|
Launch a server with CERT_REQUIRED, and check that trying to
|
|
|
|
connect to it with the given client certificate fails.
|
|
|
|
"""
|
- 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.
2008-04-08 20:47:30 -03:00
|
|
|
server = ThreadedEchoServer(CERTFILE,
|
2007-09-10 18:51:02 -03:00
|
|
|
certreqs=ssl.CERT_REQUIRED,
|
|
|
|
cacerts=CERTFILE, chatty=False)
|
|
|
|
flag = threading.Event()
|
|
|
|
server.start(flag)
|
|
|
|
# wait for it to start
|
|
|
|
flag.wait()
|
|
|
|
# try to connect
|
|
|
|
try:
|
2007-08-25 12:08:43 -03:00
|
|
|
try:
|
2007-09-10 18:51:02 -03:00
|
|
|
s = ssl.wrap_socket(socket.socket(),
|
|
|
|
certfile=certfile,
|
|
|
|
ssl_version=ssl.PROTOCOL_TLSv1)
|
- 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.
2008-04-08 20:47:30 -03:00
|
|
|
s.connect((HOST, server.port))
|
2007-09-10 18:51:02 -03:00
|
|
|
except ssl.SSLError, x:
|
2007-08-26 22:15:33 -03:00
|
|
|
if test_support.verbose:
|
2007-09-10 18:51:02 -03:00
|
|
|
sys.stdout.write("\nSSLError is %s\n" % x[1])
|
2010-04-27 10:13:26 -03:00
|
|
|
except socket.error, x:
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\nsocket.error is %s\n" % x[1])
|
2007-09-10 18:51:02 -03:00
|
|
|
else:
|
2010-05-06 11:11:23 -03:00
|
|
|
raise AssertionError("Use of invalid cert should have failed!")
|
2007-09-10 18:51:02 -03:00
|
|
|
finally:
|
|
|
|
server.stop()
|
|
|
|
server.join()
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def server_params_test(certfile, protocol, certreqs, cacertsfile,
|
|
|
|
client_certfile, client_protocol=None, indata="FOO\n",
|
|
|
|
ciphers=None, chatty=True, connectionchatty=False,
|
|
|
|
wrap_accepting_socket=False):
|
|
|
|
"""
|
|
|
|
Launch a server, connect a client to it and try various reads
|
|
|
|
and writes.
|
|
|
|
"""
|
- 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.
2008-04-08 20:47:30 -03:00
|
|
|
server = ThreadedEchoServer(certfile,
|
2007-09-10 18:51:02 -03:00
|
|
|
certreqs=certreqs,
|
|
|
|
ssl_version=protocol,
|
|
|
|
cacerts=cacertsfile,
|
2010-04-17 14:10:38 -03:00
|
|
|
ciphers=ciphers,
|
2007-09-10 18:51:02 -03:00
|
|
|
chatty=chatty,
|
2008-06-28 19:19:33 -03:00
|
|
|
connectionchatty=connectionchatty,
|
|
|
|
wrap_accepting_socket=wrap_accepting_socket)
|
2007-09-10 18:51:02 -03:00
|
|
|
flag = threading.Event()
|
|
|
|
server.start(flag)
|
|
|
|
# wait for it to start
|
|
|
|
flag.wait()
|
|
|
|
# try to connect
|
|
|
|
if client_protocol is None:
|
|
|
|
client_protocol = protocol
|
|
|
|
try:
|
2010-04-27 07:32:58 -03:00
|
|
|
s = ssl.wrap_socket(socket.socket(),
|
|
|
|
certfile=client_certfile,
|
|
|
|
ca_certs=cacertsfile,
|
|
|
|
ciphers=ciphers,
|
|
|
|
cert_reqs=certreqs,
|
|
|
|
ssl_version=client_protocol)
|
|
|
|
s.connect((HOST, server.port))
|
|
|
|
for arg in [indata, bytearray(indata), memoryview(indata)]:
|
2007-09-10 18:51:02 -03:00
|
|
|
if connectionchatty:
|
|
|
|
if test_support.verbose:
|
2010-04-27 07:32:58 -03:00
|
|
|
sys.stdout.write(
|
|
|
|
" client: sending %s...\n" % (repr(arg)))
|
|
|
|
s.write(arg)
|
|
|
|
outdata = s.read()
|
|
|
|
if connectionchatty:
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(" client: read %s\n" % repr(outdata))
|
|
|
|
if outdata != indata.lower():
|
2010-05-06 11:11:23 -03:00
|
|
|
raise AssertionError(
|
2010-04-27 07:32:58 -03:00
|
|
|
"bad data <<%s>> (%d) received; expected <<%s>> (%d)\n"
|
|
|
|
% (outdata[:min(len(outdata),20)], len(outdata),
|
|
|
|
indata[:min(len(indata),20)].lower(), len(indata)))
|
|
|
|
s.write("over\n")
|
|
|
|
if connectionchatty:
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(" client: closing connection.\n")
|
|
|
|
s.close()
|
2007-09-10 18:51:02 -03:00
|
|
|
finally:
|
|
|
|
server.stop()
|
|
|
|
server.join()
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def try_protocol_combo(server_protocol,
|
|
|
|
client_protocol,
|
|
|
|
expect_success,
|
|
|
|
certsreqs=None):
|
2008-03-29 12:24:25 -03:00
|
|
|
if certsreqs is None:
|
2007-09-10 22:09:19 -03:00
|
|
|
certsreqs = ssl.CERT_NONE
|
2010-04-28 18:11:01 -03:00
|
|
|
certtype = {
|
|
|
|
ssl.CERT_NONE: "CERT_NONE",
|
|
|
|
ssl.CERT_OPTIONAL: "CERT_OPTIONAL",
|
|
|
|
ssl.CERT_REQUIRED: "CERT_REQUIRED",
|
|
|
|
}[certsreqs]
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
2010-04-28 18:11:01 -03:00
|
|
|
formatstr = (expect_success and " %s->%s %s\n") or " {%s->%s} %s\n"
|
2007-09-10 18:51:02 -03:00
|
|
|
sys.stdout.write(formatstr %
|
|
|
|
(ssl.get_protocol_name(client_protocol),
|
|
|
|
ssl.get_protocol_name(server_protocol),
|
|
|
|
certtype))
|
|
|
|
try:
|
2010-04-17 14:10:38 -03:00
|
|
|
# NOTE: we must enable "ALL" ciphers, otherwise an SSLv23 client
|
|
|
|
# will send an SSLv3 hello (rather than SSLv2) starting from
|
|
|
|
# OpenSSL 1.0.0 (see issue #8322).
|
2010-04-28 18:11:01 -03:00
|
|
|
server_params_test(CERTFILE, server_protocol, certsreqs,
|
|
|
|
CERTFILE, CERTFILE, client_protocol,
|
|
|
|
ciphers="ALL", chatty=False)
|
2010-04-27 07:32:58 -03:00
|
|
|
# Protocol mismatch can result in either an SSLError, or a
|
|
|
|
# "Connection reset by peer" error.
|
|
|
|
except ssl.SSLError:
|
2010-04-28 18:11:01 -03:00
|
|
|
if expect_success:
|
2007-09-10 18:51:02 -03:00
|
|
|
raise
|
2010-04-27 07:32:58 -03:00
|
|
|
except socket.error as e:
|
2010-04-28 18:11:01 -03:00
|
|
|
if expect_success or e.errno != errno.ECONNRESET:
|
2010-04-27 07:32:58 -03:00
|
|
|
raise
|
2007-09-10 18:51:02 -03:00
|
|
|
else:
|
2010-04-28 18:11:01 -03:00
|
|
|
if not expect_success:
|
2010-05-06 11:11:23 -03:00
|
|
|
raise AssertionError(
|
2007-09-10 18:51:02 -03:00
|
|
|
"Client protocol %s succeeded with server protocol %s!"
|
|
|
|
% (ssl.get_protocol_name(client_protocol),
|
|
|
|
ssl.get_protocol_name(server_protocol)))
|
|
|
|
|
|
|
|
|
2008-06-28 19:19:33 -03:00
|
|
|
class ThreadedTests(unittest.TestCase):
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_rude_shutdown(self):
|
|
|
|
"""A brutal shutdown of an SSL server should raise an IOError
|
|
|
|
in the client when attempting handshake.
|
|
|
|
"""
|
2007-09-10 18:51:02 -03:00
|
|
|
listener_ready = threading.Event()
|
|
|
|
listener_gone = threading.Event()
|
|
|
|
|
2010-04-27 05:40:51 -03:00
|
|
|
s = socket.socket()
|
|
|
|
port = test_support.bind_port(s, HOST)
|
|
|
|
|
|
|
|
# `listener` runs in a thread. It 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.
|
2007-09-10 18:51:02 -03:00
|
|
|
def listener():
|
|
|
|
s.listen(5)
|
|
|
|
listener_ready.set()
|
|
|
|
s.accept()
|
2010-04-27 05:40:51 -03:00
|
|
|
s.close()
|
2007-09-10 18:51:02 -03:00
|
|
|
listener_gone.set()
|
|
|
|
|
|
|
|
def connector():
|
|
|
|
listener_ready.wait()
|
2010-04-27 05:40:51 -03:00
|
|
|
c = socket.socket()
|
|
|
|
c.connect((HOST, port))
|
2007-09-10 18:51:02 -03:00
|
|
|
listener_gone.wait()
|
|
|
|
try:
|
2010-04-27 05:40:51 -03:00
|
|
|
ssl_sock = ssl.wrap_socket(c)
|
2008-06-28 19:19:33 -03:00
|
|
|
except IOError:
|
2007-09-10 18:51:02 -03:00
|
|
|
pass
|
|
|
|
else:
|
2010-04-27 07:32:58 -03:00
|
|
|
self.fail('connecting to closed SSL socket should have failed')
|
2007-09-10 18:51:02 -03:00
|
|
|
|
|
|
|
t = threading.Thread(target=listener)
|
|
|
|
t.start()
|
2010-04-27 05:40:51 -03:00
|
|
|
try:
|
|
|
|
connector()
|
|
|
|
finally:
|
|
|
|
t.join()
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-08-04 14:38:33 -03:00
|
|
|
@skip_if_broken_ubuntu_ssl
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_echo(self):
|
|
|
|
"""Basic test of an SSL client connecting to a server"""
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
2010-04-28 18:11:01 -03:00
|
|
|
server_params_test(CERTFILE, ssl.PROTOCOL_TLSv1, ssl.CERT_NONE,
|
|
|
|
CERTFILE, CERTFILE, ssl.PROTOCOL_TLSv1,
|
|
|
|
chatty=True, connectionchatty=True)
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_getpeercert(self):
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
s2 = socket.socket()
|
- 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.
2008-04-08 20:47:30 -03:00
|
|
|
server = ThreadedEchoServer(CERTFILE,
|
2007-09-10 18:51:02 -03:00
|
|
|
certreqs=ssl.CERT_NONE,
|
|
|
|
ssl_version=ssl.PROTOCOL_SSLv23,
|
|
|
|
cacerts=CERTFILE,
|
|
|
|
chatty=False)
|
|
|
|
flag = threading.Event()
|
|
|
|
server.start(flag)
|
|
|
|
# wait for it to start
|
|
|
|
flag.wait()
|
|
|
|
# try to connect
|
|
|
|
try:
|
2010-04-27 07:32:58 -03:00
|
|
|
s = ssl.wrap_socket(socket.socket(),
|
|
|
|
certfile=CERTFILE,
|
|
|
|
ca_certs=CERTFILE,
|
|
|
|
cert_reqs=ssl.CERT_REQUIRED,
|
|
|
|
ssl_version=ssl.PROTOCOL_SSLv23)
|
|
|
|
s.connect((HOST, server.port))
|
|
|
|
cert = s.getpeercert()
|
|
|
|
self.assertTrue(cert, "Can't get peer certificate.")
|
|
|
|
cipher = s.cipher()
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(pprint.pformat(cert) + '\n')
|
|
|
|
sys.stdout.write("Connection cipher is " + str(cipher) + '.\n')
|
|
|
|
if 'subject' not in cert:
|
|
|
|
self.fail("No subject field in certificate: %s." %
|
|
|
|
pprint.pformat(cert))
|
|
|
|
if ((('organizationName', 'Python Software Foundation'),)
|
|
|
|
not in cert['subject']):
|
|
|
|
self.fail(
|
|
|
|
"Missing or invalid 'organizationName' field in certificate subject; "
|
|
|
|
"should be 'Python Software Foundation'.")
|
|
|
|
s.close()
|
2007-09-10 18:51:02 -03:00
|
|
|
finally:
|
|
|
|
server.stop()
|
|
|
|
server.join()
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_empty_cert(self):
|
|
|
|
"""Connecting with an empty cert file"""
|
|
|
|
bad_cert_test(os.path.join(os.path.dirname(__file__) or os.curdir,
|
|
|
|
"nullcert.pem"))
|
|
|
|
def test_malformed_cert(self):
|
|
|
|
"""Connecting with a badly formatted certificate (syntax error)"""
|
|
|
|
bad_cert_test(os.path.join(os.path.dirname(__file__) or os.curdir,
|
|
|
|
"badcert.pem"))
|
|
|
|
def test_nonexisting_cert(self):
|
|
|
|
"""Connecting with a non-existing cert file"""
|
|
|
|
bad_cert_test(os.path.join(os.path.dirname(__file__) or os.curdir,
|
|
|
|
"wrongcert.pem"))
|
|
|
|
def test_malformed_key(self):
|
|
|
|
"""Connecting with a badly formatted key (syntax error)"""
|
|
|
|
bad_cert_test(os.path.join(os.path.dirname(__file__) or os.curdir,
|
|
|
|
"badkey.pem"))
|
|
|
|
|
2010-08-04 14:38:33 -03:00
|
|
|
@skip_if_broken_ubuntu_ssl
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_protocol_sslv2(self):
|
|
|
|
"""Connecting to an SSLv2 server with various client options"""
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
2011-10-30 17:31:34 -03:00
|
|
|
if not hasattr(ssl, 'PROTOCOL_SSLv2'):
|
|
|
|
self.skipTest("PROTOCOL_SSLv2 needed")
|
2010-04-28 18:11:01 -03:00
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True, ssl.CERT_OPTIONAL)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv2, True, ssl.CERT_REQUIRED)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv23, True)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_SSLv3, False)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv2, ssl.PROTOCOL_TLSv1, False)
|
|
|
|
|
2010-08-04 14:38:33 -03:00
|
|
|
@skip_if_broken_ubuntu_ssl
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_protocol_sslv23(self):
|
|
|
|
"""Connecting to an SSLv23 server with various client options"""
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
2010-04-28 18:11:01 -03:00
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True)
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True, ssl.CERT_OPTIONAL)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True, ssl.CERT_OPTIONAL)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True, ssl.CERT_OPTIONAL)
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv3, True, ssl.CERT_REQUIRED)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_SSLv23, True, ssl.CERT_REQUIRED)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv23, ssl.PROTOCOL_TLSv1, True, ssl.CERT_REQUIRED)
|
2007-09-10 18:51:02 -03:00
|
|
|
|
2010-08-04 14:38:33 -03:00
|
|
|
@skip_if_broken_ubuntu_ssl
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_protocol_sslv3(self):
|
|
|
|
"""Connecting to an SSLv3 server with various client options"""
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
2010-04-28 18:11:01 -03:00
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_OPTIONAL)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv3, True, ssl.CERT_REQUIRED)
|
2011-05-09 20:52:03 -03:00
|
|
|
if hasattr(ssl, 'PROTOCOL_SSLv2'):
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_SSLv2, False)
|
2010-04-28 18:11:01 -03:00
|
|
|
try_protocol_combo(ssl.PROTOCOL_SSLv3, ssl.PROTOCOL_TLSv1, False)
|
|
|
|
|
2010-08-04 14:38:33 -03:00
|
|
|
@skip_if_broken_ubuntu_ssl
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_protocol_tlsv1(self):
|
|
|
|
"""Connecting to a TLSv1 server with various client options"""
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
2010-04-28 18:11:01 -03:00
|
|
|
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_OPTIONAL)
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_TLSv1, True, ssl.CERT_REQUIRED)
|
2011-05-09 20:52:03 -03:00
|
|
|
if hasattr(ssl, 'PROTOCOL_SSLv2'):
|
|
|
|
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv2, False)
|
2010-04-28 18:11:01 -03:00
|
|
|
try_protocol_combo(ssl.PROTOCOL_TLSv1, ssl.PROTOCOL_SSLv3, False)
|
|
|
|
|
|
|
|
def test_starttls(self):
|
|
|
|
"""Switching from clear text to encrypted and back again."""
|
2008-08-12 13:31:21 -03:00
|
|
|
msgs = ("msg 1", "MSG 2", "STARTTLS", "MSG 3", "msg 4", "ENDTLS", "msg 5", "msg 6")
|
2007-09-10 18:51:02 -03:00
|
|
|
|
- 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.
2008-04-08 20:47:30 -03:00
|
|
|
server = ThreadedEchoServer(CERTFILE,
|
2007-09-10 18:51:02 -03:00
|
|
|
ssl_version=ssl.PROTOCOL_TLSv1,
|
|
|
|
starttls_server=True,
|
|
|
|
chatty=True,
|
|
|
|
connectionchatty=True)
|
|
|
|
flag = threading.Event()
|
|
|
|
server.start(flag)
|
|
|
|
# wait for it to start
|
|
|
|
flag.wait()
|
|
|
|
# try to connect
|
|
|
|
wrapped = False
|
|
|
|
try:
|
2010-04-27 07:32:58 -03:00
|
|
|
s = socket.socket()
|
|
|
|
s.setblocking(1)
|
|
|
|
s.connect((HOST, server.port))
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
for indata in msgs:
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
2010-04-27 07:32:58 -03:00
|
|
|
sys.stdout.write(
|
|
|
|
" client: sending %s...\n" % repr(indata))
|
|
|
|
if wrapped:
|
|
|
|
conn.write(indata)
|
|
|
|
outdata = conn.read()
|
|
|
|
else:
|
|
|
|
s.send(indata)
|
|
|
|
outdata = s.recv(1024)
|
|
|
|
if (indata == "STARTTLS" and
|
|
|
|
outdata.strip().lower().startswith("ok")):
|
2010-04-28 18:11:01 -03:00
|
|
|
# STARTTLS ok, switch to secure mode
|
2007-09-10 18:51:02 -03:00
|
|
|
if test_support.verbose:
|
2007-09-16 19:06:00 -03:00
|
|
|
sys.stdout.write(
|
2010-04-27 07:32:58 -03:00
|
|
|
" client: read %s from server, starting TLS...\n"
|
|
|
|
% repr(outdata))
|
|
|
|
conn = ssl.wrap_socket(s, ssl_version=ssl.PROTOCOL_TLSv1)
|
|
|
|
wrapped = True
|
|
|
|
elif (indata == "ENDTLS" and
|
|
|
|
outdata.strip().lower().startswith("ok")):
|
2010-04-28 18:11:01 -03:00
|
|
|
# ENDTLS ok, switch back to clear text
|
2010-04-27 07:32:58 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(
|
|
|
|
" client: read %s from server, ending TLS...\n"
|
|
|
|
% repr(outdata))
|
|
|
|
s = conn.unwrap()
|
|
|
|
wrapped = False
|
2007-09-10 18:51:02 -03:00
|
|
|
else:
|
2010-04-27 07:32:58 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(
|
|
|
|
" client: read %s from server\n" % repr(outdata))
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(" client: closing connection.\n")
|
|
|
|
if wrapped:
|
|
|
|
conn.write("over\n")
|
|
|
|
else:
|
|
|
|
s.send("over\n")
|
|
|
|
s.close()
|
2007-09-10 18:51:02 -03:00
|
|
|
finally:
|
|
|
|
server.stop()
|
|
|
|
server.join()
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_socketserver(self):
|
|
|
|
"""Using a SocketServer to create and manage SSL connections."""
|
2008-06-28 19:19:33 -03:00
|
|
|
server = SocketServerHTTPSServer(CERTFILE)
|
2007-09-16 19:06:00 -03:00
|
|
|
flag = threading.Event()
|
|
|
|
server.start(flag)
|
|
|
|
# wait for it to start
|
|
|
|
flag.wait()
|
|
|
|
# try to connect
|
|
|
|
try:
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write('\n')
|
2010-04-28 18:11:01 -03:00
|
|
|
with open(CERTFILE, 'rb') as f:
|
|
|
|
d1 = f.read()
|
2007-09-16 19:06:00 -03:00
|
|
|
d2 = ''
|
|
|
|
# now fetch the same data from the HTTPS server
|
2008-06-28 19:19:33 -03:00
|
|
|
url = 'https://127.0.0.1:%d/%s' % (
|
|
|
|
server.port, os.path.split(CERTFILE)[1])
|
2010-03-20 22:14:24 -03:00
|
|
|
with test_support.check_py3k_warnings():
|
|
|
|
f = urllib.urlopen(url)
|
2007-09-16 19:06:00 -03:00
|
|
|
dlen = f.info().getheader("content-length")
|
|
|
|
if dlen and (int(dlen) > 0):
|
|
|
|
d2 = f.read(int(dlen))
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(
|
|
|
|
" client: read %d bytes from remote server '%s'\n"
|
|
|
|
% (len(d2), server))
|
|
|
|
f.close()
|
2010-04-27 07:32:58 -03:00
|
|
|
self.assertEqual(d1, d2)
|
2007-09-16 19:06:00 -03:00
|
|
|
finally:
|
|
|
|
server.stop()
|
|
|
|
server.join()
|
2007-08-26 15:50:39 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_wrapped_accept(self):
|
|
|
|
"""Check the accept() method on SSL sockets."""
|
2008-06-28 19:19:33 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
2010-04-28 18:11:01 -03:00
|
|
|
server_params_test(CERTFILE, ssl.PROTOCOL_SSLv23, ssl.CERT_REQUIRED,
|
|
|
|
CERTFILE, CERTFILE, ssl.PROTOCOL_SSLv23,
|
|
|
|
chatty=True, connectionchatty=True,
|
|
|
|
wrap_accepting_socket=True)
|
2008-06-28 19:19:33 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_asyncore_server(self):
|
|
|
|
"""Check the example asyncore integration."""
|
2008-06-28 19:19:33 -03:00
|
|
|
indata = "TEST MESSAGE of mixed case\n"
|
|
|
|
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
server = AsyncoreEchoServer(CERTFILE)
|
|
|
|
flag = threading.Event()
|
|
|
|
server.start(flag)
|
|
|
|
# wait for it to start
|
|
|
|
flag.wait()
|
|
|
|
# try to connect
|
|
|
|
try:
|
2010-04-27 07:32:58 -03:00
|
|
|
s = ssl.wrap_socket(socket.socket())
|
|
|
|
s.connect(('127.0.0.1', server.port))
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(
|
|
|
|
" client: sending %s...\n" % (repr(indata)))
|
|
|
|
s.write(indata)
|
|
|
|
outdata = s.read()
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(" client: read %s\n" % repr(outdata))
|
|
|
|
if outdata != indata.lower():
|
|
|
|
self.fail(
|
|
|
|
"bad data <<%s>> (%d) received; expected <<%s>> (%d)\n"
|
|
|
|
% (outdata[:min(len(outdata),20)], len(outdata),
|
|
|
|
indata[:min(len(indata),20)].lower(), len(indata)))
|
|
|
|
s.write("over\n")
|
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write(" client: closing connection.\n")
|
|
|
|
s.close()
|
2008-06-28 19:19:33 -03:00
|
|
|
finally:
|
|
|
|
server.stop()
|
|
|
|
# wait for server thread to end
|
|
|
|
server.join()
|
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
def test_recv_send(self):
|
|
|
|
"""Test recv(), send() and friends."""
|
2008-09-08 13:37:24 -03:00
|
|
|
if test_support.verbose:
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
|
|
|
|
server = ThreadedEchoServer(CERTFILE,
|
|
|
|
certreqs=ssl.CERT_NONE,
|
|
|
|
ssl_version=ssl.PROTOCOL_TLSv1,
|
|
|
|
cacerts=CERTFILE,
|
|
|
|
chatty=True,
|
|
|
|
connectionchatty=False)
|
|
|
|
flag = threading.Event()
|
|
|
|
server.start(flag)
|
|
|
|
# wait for it to start
|
|
|
|
flag.wait()
|
|
|
|
# try to connect
|
2010-04-27 07:32:58 -03:00
|
|
|
s = ssl.wrap_socket(socket.socket(),
|
|
|
|
server_side=False,
|
|
|
|
certfile=CERTFILE,
|
|
|
|
ca_certs=CERTFILE,
|
|
|
|
cert_reqs=ssl.CERT_NONE,
|
|
|
|
ssl_version=ssl.PROTOCOL_TLSv1)
|
|
|
|
s.connect((HOST, server.port))
|
2008-09-08 13:37:24 -03:00
|
|
|
try:
|
|
|
|
# helper methods for standardising recv* method signatures
|
|
|
|
def _recv_into():
|
|
|
|
b = bytearray("\0"*100)
|
|
|
|
count = s.recv_into(b)
|
|
|
|
return b[:count]
|
|
|
|
|
|
|
|
def _recvfrom_into():
|
|
|
|
b = bytearray("\0"*100)
|
|
|
|
count, addr = s.recvfrom_into(b)
|
|
|
|
return b[:count]
|
|
|
|
|
|
|
|
# (name, method, whether to expect success, *args)
|
|
|
|
send_methods = [
|
|
|
|
('send', s.send, True, []),
|
|
|
|
('sendto', s.sendto, False, ["some.address"]),
|
|
|
|
('sendall', s.sendall, True, []),
|
|
|
|
]
|
|
|
|
recv_methods = [
|
|
|
|
('recv', s.recv, True, []),
|
|
|
|
('recvfrom', s.recvfrom, False, ["some.address"]),
|
|
|
|
('recv_into', _recv_into, True, []),
|
|
|
|
('recvfrom_into', _recvfrom_into, False, []),
|
|
|
|
]
|
|
|
|
data_prefix = u"PREFIX_"
|
|
|
|
|
|
|
|
for meth_name, send_meth, expect_success, args in send_methods:
|
|
|
|
indata = data_prefix + meth_name
|
|
|
|
try:
|
|
|
|
send_meth(indata.encode('ASCII', 'strict'), *args)
|
|
|
|
outdata = s.read()
|
|
|
|
outdata = outdata.decode('ASCII', 'strict')
|
|
|
|
if outdata != indata.lower():
|
2010-02-06 19:23:45 -04:00
|
|
|
self.fail(
|
2008-09-08 13:37:24 -03:00
|
|
|
"While sending with <<%s>> bad data "
|
|
|
|
"<<%r>> (%d) received; "
|
|
|
|
"expected <<%r>> (%d)\n" % (
|
|
|
|
meth_name, outdata[:20], len(outdata),
|
|
|
|
indata[:20], len(indata)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except ValueError as e:
|
|
|
|
if expect_success:
|
2010-02-06 19:23:45 -04:00
|
|
|
self.fail(
|
2008-09-08 13:37:24 -03:00
|
|
|
"Failed to send with method <<%s>>; "
|
|
|
|
"expected to succeed.\n" % (meth_name,)
|
|
|
|
)
|
|
|
|
if not str(e).startswith(meth_name):
|
2010-02-06 19:23:45 -04:00
|
|
|
self.fail(
|
2008-09-08 13:37:24 -03:00
|
|
|
"Method <<%s>> failed with unexpected "
|
|
|
|
"exception message: %s\n" % (
|
|
|
|
meth_name, e
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
for meth_name, recv_meth, expect_success, args in recv_methods:
|
|
|
|
indata = data_prefix + meth_name
|
|
|
|
try:
|
|
|
|
s.send(indata.encode('ASCII', 'strict'))
|
|
|
|
outdata = recv_meth(*args)
|
|
|
|
outdata = outdata.decode('ASCII', 'strict')
|
|
|
|
if outdata != indata.lower():
|
2010-02-06 19:23:45 -04:00
|
|
|
self.fail(
|
2008-09-08 13:37:24 -03:00
|
|
|
"While receiving with <<%s>> bad data "
|
|
|
|
"<<%r>> (%d) received; "
|
|
|
|
"expected <<%r>> (%d)\n" % (
|
|
|
|
meth_name, outdata[:20], len(outdata),
|
|
|
|
indata[:20], len(indata)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except ValueError as e:
|
|
|
|
if expect_success:
|
2010-02-06 19:23:45 -04:00
|
|
|
self.fail(
|
2008-09-08 13:37:24 -03:00
|
|
|
"Failed to receive with method <<%s>>; "
|
|
|
|
"expected to succeed.\n" % (meth_name,)
|
|
|
|
)
|
|
|
|
if not str(e).startswith(meth_name):
|
2010-02-06 19:23:45 -04:00
|
|
|
self.fail(
|
2008-09-08 13:37:24 -03:00
|
|
|
"Method <<%s>> failed with unexpected "
|
|
|
|
"exception message: %s\n" % (
|
|
|
|
meth_name, e
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# consume data
|
|
|
|
s.read()
|
|
|
|
|
|
|
|
s.write("over\n".encode("ASCII", "strict"))
|
|
|
|
s.close()
|
|
|
|
finally:
|
|
|
|
server.stop()
|
|
|
|
server.join()
|
|
|
|
|
2010-04-24 17:04:58 -03:00
|
|
|
def test_handshake_timeout(self):
|
|
|
|
# Issue #5103: SSL handshake must respect the socket timeout
|
|
|
|
server = socket.socket(socket.AF_INET)
|
|
|
|
host = "127.0.0.1"
|
|
|
|
port = test_support.bind_port(server)
|
|
|
|
started = threading.Event()
|
|
|
|
finish = False
|
|
|
|
|
|
|
|
def serve():
|
|
|
|
server.listen(5)
|
|
|
|
started.set()
|
|
|
|
conns = []
|
|
|
|
while not finish:
|
|
|
|
r, w, e = select.select([server], [], [], 0.1)
|
|
|
|
if server in r:
|
|
|
|
# Let the socket hang around rather than having
|
|
|
|
# it closed by garbage collection.
|
|
|
|
conns.append(server.accept()[0])
|
|
|
|
|
|
|
|
t = threading.Thread(target=serve)
|
|
|
|
t.start()
|
|
|
|
started.wait()
|
|
|
|
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
c = socket.socket(socket.AF_INET)
|
|
|
|
c.settimeout(0.2)
|
|
|
|
c.connect((host, port))
|
|
|
|
# Will attempt handshake and time out
|
|
|
|
self.assertRaisesRegexp(ssl.SSLError, "timed out",
|
|
|
|
ssl.wrap_socket, c)
|
|
|
|
finally:
|
|
|
|
c.close()
|
|
|
|
try:
|
|
|
|
c = socket.socket(socket.AF_INET)
|
|
|
|
c.settimeout(0.2)
|
|
|
|
c = ssl.wrap_socket(c)
|
|
|
|
# Will attempt handshake and time out
|
|
|
|
self.assertRaisesRegexp(ssl.SSLError, "timed out",
|
|
|
|
c.connect, (host, port))
|
|
|
|
finally:
|
|
|
|
c.close()
|
|
|
|
finally:
|
|
|
|
finish = True
|
|
|
|
t.join()
|
|
|
|
server.close()
|
|
|
|
|
2008-09-08 13:37:24 -03:00
|
|
|
|
2007-08-26 22:15:33 -03:00
|
|
|
def test_main(verbose=False):
|
2011-10-01 14:30:58 -03:00
|
|
|
global CERTFILE, SVN_PYTHON_ORG_ROOT_CERT, NOKIACERT
|
2007-08-27 14:19:42 -03:00
|
|
|
CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir,
|
2007-09-16 19:06:00 -03:00
|
|
|
"keycert.pem")
|
|
|
|
SVN_PYTHON_ORG_ROOT_CERT = os.path.join(
|
|
|
|
os.path.dirname(__file__) or os.curdir,
|
|
|
|
"https_svn_python_org_root.pem")
|
2011-10-01 14:30:58 -03:00
|
|
|
NOKIACERT = os.path.join(os.path.dirname(__file__) or os.curdir,
|
|
|
|
"nokia.pem")
|
2007-09-16 19:06:00 -03:00
|
|
|
|
|
|
|
if (not os.path.exists(CERTFILE) or
|
2011-10-01 14:30:58 -03:00
|
|
|
not os.path.exists(SVN_PYTHON_ORG_ROOT_CERT) or
|
|
|
|
not os.path.exists(NOKIACERT)):
|
2007-09-10 18:51:02 -03:00
|
|
|
raise test_support.TestFailed("Can't read certificate files!")
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-09-14 09:54:08 -03:00
|
|
|
tests = [BasicTests, BasicSocketTests]
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2007-09-16 19:06:00 -03:00
|
|
|
if test_support.is_resource_enabled('network'):
|
2008-06-28 19:19:33 -03:00
|
|
|
tests.append(NetworkedTests)
|
2007-09-16 19:06:00 -03:00
|
|
|
|
2007-09-10 18:51:02 -03:00
|
|
|
if _have_threads:
|
|
|
|
thread_info = test_support.threading_setup()
|
2007-09-16 19:06:00 -03:00
|
|
|
if thread_info and test_support.is_resource_enabled('network'):
|
2008-06-28 19:19:33 -03:00
|
|
|
tests.append(ThreadedTests)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
2010-04-28 18:11:01 -03:00
|
|
|
try:
|
|
|
|
test_support.run_unittest(*tests)
|
|
|
|
finally:
|
|
|
|
if _have_threads:
|
|
|
|
test_support.threading_cleanup(*thread_info)
|
2007-08-25 12:08:43 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|