summaryrefslogtreecommitdiff
path: root/Lib/test/test_asyncio
diff options
context:
space:
mode:
authorNeil Aspinall <mail@neilaspinall.co.uk>2017-12-19 19:45:42 +0000
committerAndrew Svetlov <andrew.svetlov@gmail.com>2017-12-19 21:45:42 +0200
commitf7686c1f5553b24e3307506a18e18f6544de94d3 (patch)
treeeb732724e966a23a7837e824d39a2f7181183798 /Lib/test/test_asyncio
parent4b965930e8625f77cb0e821daf5cc40e85b45f84 (diff)
downloadcpython-git-f7686c1f5553b24e3307506a18e18f6544de94d3.tar.gz
bpo-29970: Add timeout for SSL handshake in asyncio
10 seconds by default.
Diffstat (limited to 'Lib/test/test_asyncio')
-rw-r--r--Lib/test/test_asyncio/test_base_events.py33
-rw-r--r--Lib/test/test_asyncio/test_sslproto.py14
2 files changed, 35 insertions, 12 deletions
diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
index 1d45cf8642..488257b341 100644
--- a/Lib/test/test_asyncio/test_base_events.py
+++ b/Lib/test/test_asyncio/test_base_events.py
@@ -1301,34 +1301,45 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
self.loop._make_ssl_transport.side_effect = mock_make_ssl_transport
ANY = mock.ANY
+ handshake_timeout = object()
# First try the default server_hostname.
self.loop._make_ssl_transport.reset_mock()
- coro = self.loop.create_connection(MyProto, 'python.org', 80, ssl=True)
+ coro = self.loop.create_connection(
+ MyProto, 'python.org', 80, ssl=True,
+ ssl_handshake_timeout=handshake_timeout)
transport, _ = self.loop.run_until_complete(coro)
transport.close()
self.loop._make_ssl_transport.assert_called_with(
ANY, ANY, ANY, ANY,
server_side=False,
- server_hostname='python.org')
+ server_hostname='python.org',
+ ssl_handshake_timeout=handshake_timeout)
# Next try an explicit server_hostname.
self.loop._make_ssl_transport.reset_mock()
- coro = self.loop.create_connection(MyProto, 'python.org', 80, ssl=True,
- server_hostname='perl.com')
+ coro = self.loop.create_connection(
+ MyProto, 'python.org', 80, ssl=True,
+ server_hostname='perl.com',
+ ssl_handshake_timeout=handshake_timeout)
transport, _ = self.loop.run_until_complete(coro)
transport.close()
self.loop._make_ssl_transport.assert_called_with(
ANY, ANY, ANY, ANY,
server_side=False,
- server_hostname='perl.com')
+ server_hostname='perl.com',
+ ssl_handshake_timeout=handshake_timeout)
# Finally try an explicit empty server_hostname.
self.loop._make_ssl_transport.reset_mock()
- coro = self.loop.create_connection(MyProto, 'python.org', 80, ssl=True,
- server_hostname='')
+ coro = self.loop.create_connection(
+ MyProto, 'python.org', 80, ssl=True,
+ server_hostname='',
+ ssl_handshake_timeout=handshake_timeout)
transport, _ = self.loop.run_until_complete(coro)
transport.close()
- self.loop._make_ssl_transport.assert_called_with(ANY, ANY, ANY, ANY,
- server_side=False,
- server_hostname='')
+ self.loop._make_ssl_transport.assert_called_with(
+ ANY, ANY, ANY, ANY,
+ server_side=False,
+ server_hostname='',
+ ssl_handshake_timeout=handshake_timeout)
def test_create_connection_no_ssl_server_hostname_errors(self):
# When not using ssl, server_hostname must be None.
@@ -1687,7 +1698,7 @@ class BaseEventLoopWithSelectorTests(test_utils.TestCase):
constants.ACCEPT_RETRY_DELAY,
# self.loop._start_serving
mock.ANY,
- MyProto, sock, None, None, mock.ANY)
+ MyProto, sock, None, None, mock.ANY, mock.ANY)
def test_call_coroutine(self):
@asyncio.coroutine
diff --git a/Lib/test/test_asyncio/test_sslproto.py b/Lib/test/test_asyncio/test_sslproto.py
index 7650fe6bd4..1c42a35128 100644
--- a/Lib/test/test_asyncio/test_sslproto.py
+++ b/Lib/test/test_asyncio/test_sslproto.py
@@ -11,6 +11,7 @@ except ImportError:
import asyncio
from asyncio import log
from asyncio import sslproto
+from asyncio import tasks
from test.test_asyncio import utils as test_utils
@@ -25,7 +26,8 @@ class SslProtoHandshakeTests(test_utils.TestCase):
def ssl_protocol(self, waiter=None):
sslcontext = test_utils.dummy_ssl_context()
app_proto = asyncio.Protocol()
- proto = sslproto.SSLProtocol(self.loop, app_proto, sslcontext, waiter)
+ proto = sslproto.SSLProtocol(self.loop, app_proto, sslcontext, waiter,
+ ssl_handshake_timeout=0.1)
self.assertIs(proto._app_transport.get_protocol(), app_proto)
self.addCleanup(proto._app_transport.close)
return proto
@@ -63,6 +65,16 @@ class SslProtoHandshakeTests(test_utils.TestCase):
with test_utils.disable_logger():
self.loop.run_until_complete(handshake_fut)
+ def test_handshake_timeout(self):
+ # bpo-29970: Check that a connection is aborted if handshake is not
+ # completed in timeout period, instead of remaining open indefinitely
+ ssl_proto = self.ssl_protocol()
+ transport = self.connection_made(ssl_proto)
+
+ with test_utils.disable_logger():
+ self.loop.run_until_complete(tasks.sleep(0.2, loop=self.loop))
+ self.assertTrue(transport.abort.called)
+
def test_eof_received_waiter(self):
waiter = asyncio.Future(loop=self.loop)
ssl_proto = self.ssl_protocol(waiter)