2015-01-14 11:56:20 -04:00
|
|
|
"""Tests for asyncio/sslproto.py."""
|
|
|
|
|
2016-06-28 11:55:36 -03:00
|
|
|
import logging
|
2018-05-28 15:50:45 -03:00
|
|
|
import socket
|
2015-01-14 11:56:20 -04:00
|
|
|
import unittest
|
|
|
|
from unittest import mock
|
2015-01-27 19:30:40 -04:00
|
|
|
try:
|
|
|
|
import ssl
|
|
|
|
except ImportError:
|
|
|
|
ssl = None
|
2015-01-14 11:56:20 -04:00
|
|
|
|
|
|
|
import asyncio
|
2016-06-28 11:55:36 -03:00
|
|
|
from asyncio import log
|
2015-01-14 11:56:20 -04:00
|
|
|
from asyncio import sslproto
|
2017-12-19 15:45:42 -04:00
|
|
|
from asyncio import tasks
|
2017-12-11 11:04:40 -04:00
|
|
|
from test.test_asyncio import utils as test_utils
|
2017-12-30 01:35:36 -04:00
|
|
|
from test.test_asyncio import functional as func_tests
|
2015-01-14 11:56:20 -04:00
|
|
|
|
|
|
|
|
2015-01-28 19:35:56 -04:00
|
|
|
@unittest.skipIf(ssl is None, 'No ssl module')
|
2015-01-14 11:56:20 -04:00
|
|
|
class SslProtoHandshakeTests(test_utils.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
2016-11-04 15:29:28 -03:00
|
|
|
super().setUp()
|
2015-01-14 11:56:20 -04:00
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
self.set_event_loop(self.loop)
|
|
|
|
|
2018-03-10 12:27:01 -04:00
|
|
|
def ssl_protocol(self, *, waiter=None, proto=None):
|
2015-01-28 19:35:56 -04:00
|
|
|
sslcontext = test_utils.dummy_ssl_context()
|
2018-03-10 12:27:01 -04:00
|
|
|
if proto is None: # app protocol
|
|
|
|
proto = asyncio.Protocol()
|
|
|
|
ssl_proto = sslproto.SSLProtocol(self.loop, proto, sslcontext, waiter,
|
|
|
|
ssl_handshake_timeout=0.1)
|
|
|
|
self.assertIs(ssl_proto._app_transport.get_protocol(), proto)
|
|
|
|
self.addCleanup(ssl_proto._app_transport.close)
|
|
|
|
return ssl_proto
|
|
|
|
|
|
|
|
def connection_made(self, ssl_proto, *, do_handshake=None):
|
2015-01-28 19:35:56 -04:00
|
|
|
transport = mock.Mock()
|
|
|
|
sslpipe = mock.Mock()
|
|
|
|
sslpipe.shutdown.return_value = b''
|
|
|
|
if do_handshake:
|
|
|
|
sslpipe.do_handshake.side_effect = do_handshake
|
|
|
|
else:
|
|
|
|
def mock_handshake(callback):
|
|
|
|
return []
|
|
|
|
sslpipe.do_handshake.side_effect = mock_handshake
|
|
|
|
with mock.patch('asyncio.sslproto._SSLPipe', return_value=sslpipe):
|
|
|
|
ssl_proto.connection_made(transport)
|
2017-06-09 18:46:14 -03:00
|
|
|
return transport
|
2015-01-28 19:35:56 -04:00
|
|
|
|
2015-01-14 11:56:20 -04:00
|
|
|
def test_cancel_handshake(self):
|
2016-04-17 02:32:47 -03:00
|
|
|
# Python issue #23197: cancelling a handshake must not raise an
|
2015-01-14 11:56:20 -04:00
|
|
|
# exception or log an error, even if the handshake failed
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2018-03-10 12:27:01 -04:00
|
|
|
ssl_proto = self.ssl_protocol(waiter=waiter)
|
2015-01-14 11:56:20 -04:00
|
|
|
handshake_fut = asyncio.Future(loop=self.loop)
|
|
|
|
|
|
|
|
def do_handshake(callback):
|
|
|
|
exc = Exception()
|
|
|
|
callback(exc)
|
|
|
|
handshake_fut.set_result(None)
|
|
|
|
return []
|
|
|
|
|
|
|
|
waiter.cancel()
|
2018-03-10 12:27:01 -04:00
|
|
|
self.connection_made(ssl_proto, do_handshake=do_handshake)
|
2015-01-14 11:56:20 -04:00
|
|
|
|
|
|
|
with test_utils.disable_logger():
|
|
|
|
self.loop.run_until_complete(handshake_fut)
|
|
|
|
|
2017-12-19 15:45:42 -04:00
|
|
|
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)
|
|
|
|
|
2017-12-20 14:24:43 -04:00
|
|
|
def test_handshake_timeout_zero(self):
|
|
|
|
sslcontext = test_utils.dummy_ssl_context()
|
|
|
|
app_proto = mock.Mock()
|
|
|
|
waiter = mock.Mock()
|
|
|
|
with self.assertRaisesRegex(ValueError, 'a positive number'):
|
|
|
|
sslproto.SSLProtocol(self.loop, app_proto, sslcontext, waiter,
|
|
|
|
ssl_handshake_timeout=0)
|
|
|
|
|
|
|
|
def test_handshake_timeout_negative(self):
|
|
|
|
sslcontext = test_utils.dummy_ssl_context()
|
|
|
|
app_proto = mock.Mock()
|
|
|
|
waiter = mock.Mock()
|
|
|
|
with self.assertRaisesRegex(ValueError, 'a positive number'):
|
|
|
|
sslproto.SSLProtocol(self.loop, app_proto, sslcontext, waiter,
|
|
|
|
ssl_handshake_timeout=-10)
|
|
|
|
|
2015-01-28 19:35:56 -04:00
|
|
|
def test_eof_received_waiter(self):
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2018-03-10 12:27:01 -04:00
|
|
|
ssl_proto = self.ssl_protocol(waiter=waiter)
|
2015-01-28 19:35:56 -04:00
|
|
|
self.connection_made(ssl_proto)
|
|
|
|
ssl_proto.eof_received()
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIsInstance(waiter.exception(), ConnectionResetError)
|
|
|
|
|
2016-06-28 11:55:36 -03:00
|
|
|
def test_fatal_error_no_name_error(self):
|
|
|
|
# From issue #363.
|
|
|
|
# _fatal_error() generates a NameError if sslproto.py
|
|
|
|
# does not import base_events.
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2018-03-10 12:27:01 -04:00
|
|
|
ssl_proto = self.ssl_protocol(waiter=waiter)
|
2016-06-28 11:55:36 -03:00
|
|
|
# Temporarily turn off error logging so as not to spoil test output.
|
|
|
|
log_level = log.logger.getEffectiveLevel()
|
|
|
|
log.logger.setLevel(logging.FATAL)
|
|
|
|
try:
|
|
|
|
ssl_proto._fatal_error(None)
|
|
|
|
finally:
|
|
|
|
# Restore error logging.
|
|
|
|
log.logger.setLevel(log_level)
|
2015-01-14 11:56:20 -04:00
|
|
|
|
2016-12-16 12:50:41 -04:00
|
|
|
def test_connection_lost(self):
|
|
|
|
# From issue #472.
|
|
|
|
# yield from waiter hang if lost_connection was called.
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2018-03-10 12:27:01 -04:00
|
|
|
ssl_proto = self.ssl_protocol(waiter=waiter)
|
2016-12-16 12:50:41 -04:00
|
|
|
self.connection_made(ssl_proto)
|
|
|
|
ssl_proto.connection_lost(ConnectionAbortedError)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
self.assertIsInstance(waiter.exception(), ConnectionAbortedError)
|
|
|
|
|
2017-06-09 18:46:14 -03:00
|
|
|
def test_close_during_handshake(self):
|
|
|
|
# bpo-29743 Closing transport during handshake process leaks socket
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2018-03-10 12:27:01 -04:00
|
|
|
ssl_proto = self.ssl_protocol(waiter=waiter)
|
2017-06-09 18:46:14 -03:00
|
|
|
|
|
|
|
transport = self.connection_made(ssl_proto)
|
|
|
|
test_utils.run_briefly(self.loop)
|
|
|
|
|
|
|
|
ssl_proto._app_transport.close()
|
|
|
|
self.assertTrue(transport.abort.called)
|
|
|
|
|
2017-03-12 16:23:30 -03:00
|
|
|
def test_get_extra_info_on_closed_connection(self):
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2018-03-10 12:27:01 -04:00
|
|
|
ssl_proto = self.ssl_protocol(waiter=waiter)
|
2017-03-12 16:23:30 -03:00
|
|
|
self.assertIsNone(ssl_proto._get_extra_info('socket'))
|
|
|
|
default = object()
|
|
|
|
self.assertIs(ssl_proto._get_extra_info('socket', default), default)
|
|
|
|
self.connection_made(ssl_proto)
|
|
|
|
self.assertIsNotNone(ssl_proto._get_extra_info('socket'))
|
|
|
|
ssl_proto.connection_lost(None)
|
|
|
|
self.assertIsNone(ssl_proto._get_extra_info('socket'))
|
|
|
|
|
2017-10-19 14:49:57 -03:00
|
|
|
def test_set_new_app_protocol(self):
|
|
|
|
waiter = asyncio.Future(loop=self.loop)
|
2018-03-10 12:27:01 -04:00
|
|
|
ssl_proto = self.ssl_protocol(waiter=waiter)
|
2017-10-19 14:49:57 -03:00
|
|
|
new_app_proto = asyncio.Protocol()
|
|
|
|
ssl_proto._app_transport.set_protocol(new_app_proto)
|
|
|
|
self.assertIs(ssl_proto._app_transport.get_protocol(), new_app_proto)
|
|
|
|
self.assertIs(ssl_proto._app_protocol, new_app_proto)
|
|
|
|
|
2018-03-10 12:27:01 -04:00
|
|
|
def test_data_received_after_closing(self):
|
|
|
|
ssl_proto = self.ssl_protocol()
|
|
|
|
self.connection_made(ssl_proto)
|
|
|
|
transp = ssl_proto._app_transport
|
|
|
|
|
|
|
|
transp.close()
|
|
|
|
|
|
|
|
# should not raise
|
|
|
|
self.assertIsNone(ssl_proto.data_received(b'data'))
|
|
|
|
|
|
|
|
def test_write_after_closing(self):
|
|
|
|
ssl_proto = self.ssl_protocol()
|
|
|
|
self.connection_made(ssl_proto)
|
|
|
|
transp = ssl_proto._app_transport
|
|
|
|
transp.close()
|
|
|
|
|
|
|
|
# should not raise
|
|
|
|
self.assertIsNone(transp.write(b'data'))
|
|
|
|
|
2017-03-12 16:23:30 -03:00
|
|
|
|
2017-12-30 01:35:36 -04:00
|
|
|
##############################################################################
|
|
|
|
# Start TLS Tests
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
class BaseStartTLS(func_tests.FunctionalTestCaseMixin):
|
|
|
|
|
2018-05-28 15:50:45 -03:00
|
|
|
PAYLOAD_SIZE = 1024 * 100
|
|
|
|
TIMEOUT = 60
|
|
|
|
|
2017-12-30 01:35:36 -04:00
|
|
|
def new_loop(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-05-28 15:50:45 -03:00
|
|
|
def test_buf_feed_data(self):
|
|
|
|
|
|
|
|
class Proto(asyncio.BufferedProtocol):
|
|
|
|
|
|
|
|
def __init__(self, bufsize, usemv):
|
|
|
|
self.buf = bytearray(bufsize)
|
|
|
|
self.mv = memoryview(self.buf)
|
|
|
|
self.data = b''
|
|
|
|
self.usemv = usemv
|
|
|
|
|
|
|
|
def get_buffer(self, sizehint):
|
|
|
|
if self.usemv:
|
|
|
|
return self.mv
|
|
|
|
else:
|
|
|
|
return self.buf
|
|
|
|
|
|
|
|
def buffer_updated(self, nsize):
|
|
|
|
if self.usemv:
|
|
|
|
self.data += self.mv[:nsize]
|
|
|
|
else:
|
|
|
|
self.data += self.buf[:nsize]
|
|
|
|
|
|
|
|
for usemv in [False, True]:
|
|
|
|
proto = Proto(1, usemv)
|
|
|
|
sslproto._feed_data_to_bufferred_proto(proto, b'12345')
|
|
|
|
self.assertEqual(proto.data, b'12345')
|
|
|
|
|
|
|
|
proto = Proto(2, usemv)
|
|
|
|
sslproto._feed_data_to_bufferred_proto(proto, b'12345')
|
|
|
|
self.assertEqual(proto.data, b'12345')
|
|
|
|
|
|
|
|
proto = Proto(2, usemv)
|
|
|
|
sslproto._feed_data_to_bufferred_proto(proto, b'1234')
|
|
|
|
self.assertEqual(proto.data, b'1234')
|
|
|
|
|
|
|
|
proto = Proto(4, usemv)
|
|
|
|
sslproto._feed_data_to_bufferred_proto(proto, b'1234')
|
|
|
|
self.assertEqual(proto.data, b'1234')
|
|
|
|
|
|
|
|
proto = Proto(100, usemv)
|
|
|
|
sslproto._feed_data_to_bufferred_proto(proto, b'12345')
|
|
|
|
self.assertEqual(proto.data, b'12345')
|
|
|
|
|
|
|
|
proto = Proto(0, usemv)
|
|
|
|
with self.assertRaisesRegex(RuntimeError, 'empty buffer'):
|
|
|
|
sslproto._feed_data_to_bufferred_proto(proto, b'12345')
|
|
|
|
|
|
|
|
def test_start_tls_client_reg_proto_1(self):
|
|
|
|
HELLO_MSG = b'1' * self.PAYLOAD_SIZE
|
2017-12-30 01:35:36 -04:00
|
|
|
|
|
|
|
server_context = test_utils.simple_server_sslcontext()
|
|
|
|
client_context = test_utils.simple_client_sslcontext()
|
|
|
|
|
|
|
|
def serve(sock):
|
2018-05-28 15:50:45 -03:00
|
|
|
sock.settimeout(self.TIMEOUT)
|
2018-01-29 01:25:05 -04:00
|
|
|
|
2017-12-30 01:35:36 -04:00
|
|
|
data = sock.recv_all(len(HELLO_MSG))
|
|
|
|
self.assertEqual(len(data), len(HELLO_MSG))
|
|
|
|
|
|
|
|
sock.start_tls(server_context, server_side=True)
|
|
|
|
|
|
|
|
sock.sendall(b'O')
|
|
|
|
data = sock.recv_all(len(HELLO_MSG))
|
|
|
|
self.assertEqual(len(data), len(HELLO_MSG))
|
2018-05-28 15:50:45 -03:00
|
|
|
|
|
|
|
sock.shutdown(socket.SHUT_RDWR)
|
2017-12-30 01:35:36 -04:00
|
|
|
sock.close()
|
|
|
|
|
|
|
|
class ClientProto(asyncio.Protocol):
|
|
|
|
def __init__(self, on_data, on_eof):
|
|
|
|
self.on_data = on_data
|
|
|
|
self.on_eof = on_eof
|
|
|
|
self.con_made_cnt = 0
|
|
|
|
|
|
|
|
def connection_made(proto, tr):
|
|
|
|
proto.con_made_cnt += 1
|
|
|
|
# Ensure connection_made gets called only once.
|
|
|
|
self.assertEqual(proto.con_made_cnt, 1)
|
|
|
|
|
|
|
|
def data_received(self, data):
|
|
|
|
self.on_data.set_result(data)
|
|
|
|
|
|
|
|
def eof_received(self):
|
|
|
|
self.on_eof.set_result(True)
|
|
|
|
|
|
|
|
async def client(addr):
|
2018-01-29 01:25:05 -04:00
|
|
|
await asyncio.sleep(0.5, loop=self.loop)
|
|
|
|
|
2017-12-30 01:35:36 -04:00
|
|
|
on_data = self.loop.create_future()
|
|
|
|
on_eof = self.loop.create_future()
|
|
|
|
|
|
|
|
tr, proto = await self.loop.create_connection(
|
|
|
|
lambda: ClientProto(on_data, on_eof), *addr)
|
|
|
|
|
|
|
|
tr.write(HELLO_MSG)
|
|
|
|
new_tr = await self.loop.start_tls(tr, proto, client_context)
|
|
|
|
|
|
|
|
self.assertEqual(await on_data, b'O')
|
|
|
|
new_tr.write(HELLO_MSG)
|
|
|
|
await on_eof
|
|
|
|
|
|
|
|
new_tr.close()
|
|
|
|
|
2018-05-28 17:44:12 -03:00
|
|
|
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv:
|
2017-12-30 01:35:36 -04:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
asyncio.wait_for(client(srv.addr), loop=self.loop, timeout=10))
|
|
|
|
|
2018-05-28 15:50:45 -03:00
|
|
|
def test_start_tls_client_buf_proto_1(self):
|
|
|
|
HELLO_MSG = b'1' * self.PAYLOAD_SIZE
|
|
|
|
|
|
|
|
server_context = test_utils.simple_server_sslcontext()
|
|
|
|
client_context = test_utils.simple_client_sslcontext()
|
|
|
|
|
|
|
|
def serve(sock):
|
|
|
|
sock.settimeout(self.TIMEOUT)
|
|
|
|
|
|
|
|
data = sock.recv_all(len(HELLO_MSG))
|
|
|
|
self.assertEqual(len(data), len(HELLO_MSG))
|
|
|
|
|
|
|
|
sock.start_tls(server_context, server_side=True)
|
|
|
|
|
|
|
|
sock.sendall(b'O')
|
|
|
|
data = sock.recv_all(len(HELLO_MSG))
|
|
|
|
self.assertEqual(len(data), len(HELLO_MSG))
|
|
|
|
|
|
|
|
sock.shutdown(socket.SHUT_RDWR)
|
|
|
|
sock.close()
|
|
|
|
|
|
|
|
class ClientProto(asyncio.BufferedProtocol):
|
|
|
|
def __init__(self, on_data, on_eof):
|
|
|
|
self.on_data = on_data
|
|
|
|
self.on_eof = on_eof
|
|
|
|
self.con_made_cnt = 0
|
|
|
|
self.buf = bytearray(1)
|
|
|
|
|
|
|
|
def connection_made(proto, tr):
|
|
|
|
proto.con_made_cnt += 1
|
|
|
|
# Ensure connection_made gets called only once.
|
|
|
|
self.assertEqual(proto.con_made_cnt, 1)
|
|
|
|
|
|
|
|
def get_buffer(self, sizehint):
|
|
|
|
return self.buf
|
|
|
|
|
|
|
|
def buffer_updated(self, nsize):
|
|
|
|
assert nsize == 1
|
|
|
|
self.on_data.set_result(bytes(self.buf[:nsize]))
|
|
|
|
|
|
|
|
def eof_received(self):
|
|
|
|
self.on_eof.set_result(True)
|
|
|
|
|
|
|
|
async def client(addr):
|
|
|
|
await asyncio.sleep(0.5, loop=self.loop)
|
|
|
|
|
|
|
|
on_data = self.loop.create_future()
|
|
|
|
on_eof = self.loop.create_future()
|
|
|
|
|
|
|
|
tr, proto = await self.loop.create_connection(
|
|
|
|
lambda: ClientProto(on_data, on_eof), *addr)
|
|
|
|
|
|
|
|
tr.write(HELLO_MSG)
|
|
|
|
new_tr = await self.loop.start_tls(tr, proto, client_context)
|
|
|
|
|
|
|
|
self.assertEqual(await on_data, b'O')
|
|
|
|
new_tr.write(HELLO_MSG)
|
|
|
|
await on_eof
|
|
|
|
|
|
|
|
new_tr.close()
|
|
|
|
|
2018-05-28 17:44:12 -03:00
|
|
|
with self.tcp_server(serve, timeout=self.TIMEOUT) as srv:
|
2018-05-28 15:50:45 -03:00
|
|
|
self.loop.run_until_complete(
|
|
|
|
asyncio.wait_for(client(srv.addr),
|
|
|
|
loop=self.loop, timeout=self.TIMEOUT))
|
|
|
|
|
2017-12-30 01:35:36 -04:00
|
|
|
def test_start_tls_server_1(self):
|
2018-05-28 15:50:45 -03:00
|
|
|
HELLO_MSG = b'1' * self.PAYLOAD_SIZE
|
2017-12-30 01:35:36 -04:00
|
|
|
|
|
|
|
server_context = test_utils.simple_server_sslcontext()
|
|
|
|
client_context = test_utils.simple_client_sslcontext()
|
|
|
|
|
|
|
|
def client(sock, addr):
|
2018-05-28 15:50:45 -03:00
|
|
|
sock.settimeout(self.TIMEOUT)
|
2018-01-29 01:25:05 -04:00
|
|
|
|
2017-12-30 01:35:36 -04:00
|
|
|
sock.connect(addr)
|
|
|
|
data = sock.recv_all(len(HELLO_MSG))
|
|
|
|
self.assertEqual(len(data), len(HELLO_MSG))
|
|
|
|
|
|
|
|
sock.start_tls(client_context)
|
|
|
|
sock.sendall(HELLO_MSG)
|
2018-05-28 15:50:45 -03:00
|
|
|
|
|
|
|
sock.shutdown(socket.SHUT_RDWR)
|
2017-12-30 01:35:36 -04:00
|
|
|
sock.close()
|
|
|
|
|
|
|
|
class ServerProto(asyncio.Protocol):
|
2018-05-28 15:50:45 -03:00
|
|
|
def __init__(self, on_con, on_eof, on_con_lost):
|
2017-12-30 01:35:36 -04:00
|
|
|
self.on_con = on_con
|
|
|
|
self.on_eof = on_eof
|
2018-05-28 15:50:45 -03:00
|
|
|
self.on_con_lost = on_con_lost
|
2017-12-30 01:35:36 -04:00
|
|
|
self.data = b''
|
|
|
|
|
|
|
|
def connection_made(self, tr):
|
|
|
|
self.on_con.set_result(tr)
|
|
|
|
|
|
|
|
def data_received(self, data):
|
|
|
|
self.data += data
|
|
|
|
|
|
|
|
def eof_received(self):
|
|
|
|
self.on_eof.set_result(1)
|
|
|
|
|
2018-05-28 15:50:45 -03:00
|
|
|
def connection_lost(self, exc):
|
|
|
|
if exc is None:
|
|
|
|
self.on_con_lost.set_result(None)
|
|
|
|
else:
|
|
|
|
self.on_con_lost.set_exception(exc)
|
|
|
|
|
|
|
|
async def main(proto, on_con, on_eof, on_con_lost):
|
2017-12-30 01:35:36 -04:00
|
|
|
tr = await on_con
|
|
|
|
tr.write(HELLO_MSG)
|
|
|
|
|
|
|
|
self.assertEqual(proto.data, b'')
|
|
|
|
|
|
|
|
new_tr = await self.loop.start_tls(
|
|
|
|
tr, proto, server_context,
|
2018-05-28 17:44:12 -03:00
|
|
|
server_side=True,
|
|
|
|
ssl_handshake_timeout=self.TIMEOUT)
|
2017-12-30 01:35:36 -04:00
|
|
|
|
|
|
|
await on_eof
|
2018-05-28 15:50:45 -03:00
|
|
|
await on_con_lost
|
2017-12-30 01:35:36 -04:00
|
|
|
self.assertEqual(proto.data, HELLO_MSG)
|
|
|
|
new_tr.close()
|
|
|
|
|
2018-05-28 15:50:45 -03:00
|
|
|
async def run_main():
|
|
|
|
on_con = self.loop.create_future()
|
|
|
|
on_eof = self.loop.create_future()
|
|
|
|
on_con_lost = self.loop.create_future()
|
|
|
|
proto = ServerProto(on_con, on_eof, on_con_lost)
|
2017-12-30 01:35:36 -04:00
|
|
|
|
2018-05-28 15:50:45 -03:00
|
|
|
server = await self.loop.create_server(
|
|
|
|
lambda: proto, '127.0.0.1', 0)
|
|
|
|
addr = server.sockets[0].getsockname()
|
2017-12-30 01:35:36 -04:00
|
|
|
|
2018-05-28 17:44:12 -03:00
|
|
|
with self.tcp_client(lambda sock: client(sock, addr),
|
|
|
|
timeout=self.TIMEOUT):
|
2018-05-28 15:50:45 -03:00
|
|
|
await asyncio.wait_for(
|
|
|
|
main(proto, on_con, on_eof, on_con_lost),
|
|
|
|
loop=self.loop, timeout=self.TIMEOUT)
|
2017-12-30 01:35:36 -04:00
|
|
|
|
2018-05-28 15:50:45 -03:00
|
|
|
server.close()
|
|
|
|
await server.wait_closed()
|
|
|
|
|
|
|
|
self.loop.run_until_complete(run_main())
|
2017-12-30 01:35:36 -04:00
|
|
|
|
|
|
|
def test_start_tls_wrong_args(self):
|
|
|
|
async def main():
|
|
|
|
with self.assertRaisesRegex(TypeError, 'SSLContext, got'):
|
|
|
|
await self.loop.start_tls(None, None, None)
|
|
|
|
|
|
|
|
sslctx = test_utils.simple_server_sslcontext()
|
|
|
|
with self.assertRaisesRegex(TypeError, 'is not supported'):
|
|
|
|
await self.loop.start_tls(None, None, sslctx)
|
|
|
|
|
|
|
|
self.loop.run_until_complete(main())
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(ssl is None, 'No ssl module')
|
2017-12-30 16:40:20 -04:00
|
|
|
class SelectorStartTLSTests(BaseStartTLS, unittest.TestCase):
|
2017-12-30 01:35:36 -04:00
|
|
|
|
|
|
|
def new_loop(self):
|
|
|
|
return asyncio.SelectorEventLoop()
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(ssl is None, 'No ssl module')
|
|
|
|
@unittest.skipUnless(hasattr(asyncio, 'ProactorEventLoop'), 'Windows only')
|
2017-12-30 16:40:20 -04:00
|
|
|
class ProactorStartTLSTests(BaseStartTLS, unittest.TestCase):
|
2017-12-30 01:35:36 -04:00
|
|
|
|
|
|
|
def new_loop(self):
|
|
|
|
return asyncio.ProactorEventLoop()
|
|
|
|
|
|
|
|
|
2015-01-14 11:56:20 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|