Commit 35bebe12 authored by Antoine Pitrou's avatar Antoine Pitrou

Add a reasonable timeout to network SSL tests, so as to avoid buildbot timeouts

when the remote server doesn't answer.
parent cc5943d3
...@@ -295,10 +295,12 @@ class ContextTests(unittest.TestCase): ...@@ -295,10 +295,12 @@ class ContextTests(unittest.TestCase):
class NetworkedTests(unittest.TestCase): class NetworkedTests(unittest.TestCase):
timeout = 30
def test_connect(self): def test_connect(self):
s = ssl.wrap_socket(socket.socket(socket.AF_INET), s = ssl.wrap_socket(socket.socket(socket.AF_INET),
cert_reqs=ssl.CERT_NONE) cert_reqs=ssl.CERT_NONE)
s.settimeout(self.timeout)
try: try:
s.connect(("svn.python.org", 443)) s.connect(("svn.python.org", 443))
self.assertEqual({}, s.getpeercert()) self.assertEqual({}, s.getpeercert())
...@@ -308,6 +310,7 @@ class NetworkedTests(unittest.TestCase): ...@@ -308,6 +310,7 @@ class NetworkedTests(unittest.TestCase):
# this should fail because we have no verification certs # this should fail because we have no verification certs
s = ssl.wrap_socket(socket.socket(socket.AF_INET), s = ssl.wrap_socket(socket.socket(socket.AF_INET),
cert_reqs=ssl.CERT_REQUIRED) cert_reqs=ssl.CERT_REQUIRED)
s.settimeout(self.timeout)
self.assertRaisesRegexp(ssl.SSLError, "certificate verify failed", self.assertRaisesRegexp(ssl.SSLError, "certificate verify failed",
s.connect, ("svn.python.org", 443)) s.connect, ("svn.python.org", 443))
s.close() s.close()
...@@ -316,6 +319,7 @@ class NetworkedTests(unittest.TestCase): ...@@ -316,6 +319,7 @@ class NetworkedTests(unittest.TestCase):
s = ssl.wrap_socket(socket.socket(socket.AF_INET), s = ssl.wrap_socket(socket.socket(socket.AF_INET),
cert_reqs=ssl.CERT_REQUIRED, cert_reqs=ssl.CERT_REQUIRED,
ca_certs=SVN_PYTHON_ORG_ROOT_CERT) ca_certs=SVN_PYTHON_ORG_ROOT_CERT)
s.settimeout(self.timeout)
try: try:
s.connect(("svn.python.org", 443)) s.connect(("svn.python.org", 443))
self.assertTrue(s.getpeercert()) self.assertTrue(s.getpeercert())
...@@ -326,6 +330,7 @@ class NetworkedTests(unittest.TestCase): ...@@ -326,6 +330,7 @@ class NetworkedTests(unittest.TestCase):
# Same as test_connect, but with a separately created context # Same as test_connect, but with a separately created context
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23) ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
s = ctx.wrap_socket(socket.socket(socket.AF_INET)) s = ctx.wrap_socket(socket.socket(socket.AF_INET))
s.settimeout(self.timeout)
s.connect(("svn.python.org", 443)) s.connect(("svn.python.org", 443))
try: try:
self.assertEqual({}, s.getpeercert()) self.assertEqual({}, s.getpeercert())
...@@ -334,12 +339,14 @@ class NetworkedTests(unittest.TestCase): ...@@ -334,12 +339,14 @@ class NetworkedTests(unittest.TestCase):
# This should fail because we have no verification certs # This should fail because we have no verification certs
ctx.verify_mode = ssl.CERT_REQUIRED ctx.verify_mode = ssl.CERT_REQUIRED
s = ctx.wrap_socket(socket.socket(socket.AF_INET)) s = ctx.wrap_socket(socket.socket(socket.AF_INET))
s.settimeout(self.timeout)
self.assertRaisesRegexp(ssl.SSLError, "certificate verify failed", self.assertRaisesRegexp(ssl.SSLError, "certificate verify failed",
s.connect, ("svn.python.org", 443)) s.connect, ("svn.python.org", 443))
s.close() s.close()
# This should succeed because we specify the root cert # This should succeed because we specify the root cert
ctx.load_verify_locations(SVN_PYTHON_ORG_ROOT_CERT) ctx.load_verify_locations(SVN_PYTHON_ORG_ROOT_CERT)
s = ctx.wrap_socket(socket.socket(socket.AF_INET)) s = ctx.wrap_socket(socket.socket(socket.AF_INET))
s.settimeout(self.timeout)
s.connect(("svn.python.org", 443)) s.connect(("svn.python.org", 443))
try: try:
cert = s.getpeercert() cert = s.getpeercert()
...@@ -357,6 +364,7 @@ class NetworkedTests(unittest.TestCase): ...@@ -357,6 +364,7 @@ class NetworkedTests(unittest.TestCase):
ctx.verify_mode = ssl.CERT_REQUIRED ctx.verify_mode = ssl.CERT_REQUIRED
ctx.load_verify_locations(capath=CAPATH) ctx.load_verify_locations(capath=CAPATH)
s = ctx.wrap_socket(socket.socket(socket.AF_INET)) s = ctx.wrap_socket(socket.socket(socket.AF_INET))
s.settimeout(self.timeout)
s.connect(("svn.python.org", 443)) s.connect(("svn.python.org", 443))
try: try:
cert = s.getpeercert() cert = s.getpeercert()
...@@ -368,6 +376,7 @@ class NetworkedTests(unittest.TestCase): ...@@ -368,6 +376,7 @@ class NetworkedTests(unittest.TestCase):
ctx.verify_mode = ssl.CERT_REQUIRED ctx.verify_mode = ssl.CERT_REQUIRED
ctx.load_verify_locations(capath=BYTES_CAPATH) ctx.load_verify_locations(capath=BYTES_CAPATH)
s = ctx.wrap_socket(socket.socket(socket.AF_INET)) s = ctx.wrap_socket(socket.socket(socket.AF_INET))
s.settimeout(self.timeout)
s.connect(("svn.python.org", 443)) s.connect(("svn.python.org", 443))
try: try:
cert = s.getpeercert() cert = s.getpeercert()
...@@ -381,6 +390,7 @@ class NetworkedTests(unittest.TestCase): ...@@ -381,6 +390,7 @@ class NetworkedTests(unittest.TestCase):
# delay closing the underlying "real socket" (here tested with its # delay closing the underlying "real socket" (here tested with its
# file descriptor, hence skipping the test under Windows). # file descriptor, hence skipping the test under Windows).
ss = ssl.wrap_socket(socket.socket(socket.AF_INET)) ss = ssl.wrap_socket(socket.socket(socket.AF_INET))
ss.settimeout(self.timeout)
ss.connect(("svn.python.org", 443)) ss.connect(("svn.python.org", 443))
fd = ss.fileno() fd = ss.fileno()
f = ss.makefile() f = ss.makefile()
...@@ -396,6 +406,7 @@ class NetworkedTests(unittest.TestCase): ...@@ -396,6 +406,7 @@ class NetworkedTests(unittest.TestCase):
def test_non_blocking_handshake(self): def test_non_blocking_handshake(self):
s = socket.socket(socket.AF_INET) s = socket.socket(socket.AF_INET)
s.settimeout(self.timeout)
s.connect(("svn.python.org", 443)) s.connect(("svn.python.org", 443))
s.setblocking(False) s.setblocking(False)
s = ssl.wrap_socket(s, s = ssl.wrap_socket(s,
...@@ -452,6 +463,7 @@ class NetworkedTests(unittest.TestCase): ...@@ -452,6 +463,7 @@ class NetworkedTests(unittest.TestCase):
s = ssl.wrap_socket(socket.socket(socket.AF_INET), s = ssl.wrap_socket(socket.socket(socket.AF_INET),
cert_reqs=ssl.CERT_REQUIRED, cert_reqs=ssl.CERT_REQUIRED,
ca_certs=sha256_cert,) ca_certs=sha256_cert,)
s.settimeout(self.timeout)
with support.transient_internet(): with support.transient_internet():
try: try:
s.connect(remote) s.connect(remote)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment