| 
									
										
										
										
											2015-01-14 16:56:20 +01:00
										 |  |  | """Tests for asyncio/sslproto.py.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import unittest | 
					
						
							|  |  |  | from unittest import mock | 
					
						
							| 
									
										
										
										
											2015-01-28 00:30:40 +01:00
										 |  |  | try: | 
					
						
							|  |  |  |     import ssl | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     ssl = None | 
					
						
							| 
									
										
										
										
											2015-01-14 16:56:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import asyncio | 
					
						
							|  |  |  | from asyncio import sslproto | 
					
						
							|  |  |  | from asyncio import test_utils | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 00:35:56 +01:00
										 |  |  | @unittest.skipIf(ssl is None, 'No ssl module') | 
					
						
							| 
									
										
										
										
											2015-01-14 16:56:20 +01:00
										 |  |  | class SslProtoHandshakeTests(test_utils.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def setUp(self): | 
					
						
							|  |  |  |         self.loop = asyncio.new_event_loop() | 
					
						
							|  |  |  |         self.set_event_loop(self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 00:35:56 +01:00
										 |  |  |     def ssl_protocol(self, waiter=None): | 
					
						
							|  |  |  |         sslcontext = test_utils.dummy_ssl_context() | 
					
						
							|  |  |  |         app_proto = asyncio.Protocol() | 
					
						
							| 
									
										
										
										
											2015-01-29 17:50:58 +01:00
										 |  |  |         proto = sslproto.SSLProtocol(self.loop, app_proto, sslcontext, waiter) | 
					
						
							|  |  |  |         self.addCleanup(proto._app_transport.close) | 
					
						
							|  |  |  |         return proto | 
					
						
							| 
									
										
										
										
											2015-01-29 00:35:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def connection_made(self, ssl_proto, do_handshake=None): | 
					
						
							|  |  |  |         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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 16:56:20 +01:00
										 |  |  |     def test_cancel_handshake(self): | 
					
						
							|  |  |  |         # Python issue #23197: cancelling an handshake must not raise an | 
					
						
							|  |  |  |         # exception or log an error, even if the handshake failed | 
					
						
							|  |  |  |         waiter = asyncio.Future(loop=self.loop) | 
					
						
							| 
									
										
										
										
											2015-01-29 00:35:56 +01:00
										 |  |  |         ssl_proto = self.ssl_protocol(waiter) | 
					
						
							| 
									
										
										
										
											2015-01-14 16:56:20 +01:00
										 |  |  |         handshake_fut = asyncio.Future(loop=self.loop) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def do_handshake(callback): | 
					
						
							|  |  |  |             exc = Exception() | 
					
						
							|  |  |  |             callback(exc) | 
					
						
							|  |  |  |             handshake_fut.set_result(None) | 
					
						
							|  |  |  |             return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         waiter.cancel() | 
					
						
							| 
									
										
										
										
											2015-01-29 00:35:56 +01:00
										 |  |  |         self.connection_made(ssl_proto, do_handshake) | 
					
						
							| 
									
										
										
										
											2015-01-14 16:56:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         with test_utils.disable_logger(): | 
					
						
							|  |  |  |             self.loop.run_until_complete(handshake_fut) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 00:35:56 +01:00
										 |  |  |     def test_eof_received_waiter(self): | 
					
						
							|  |  |  |         waiter = asyncio.Future(loop=self.loop) | 
					
						
							|  |  |  |         ssl_proto = self.ssl_protocol(waiter) | 
					
						
							|  |  |  |         self.connection_made(ssl_proto) | 
					
						
							|  |  |  |         ssl_proto.eof_received() | 
					
						
							|  |  |  |         test_utils.run_briefly(self.loop) | 
					
						
							|  |  |  |         self.assertIsInstance(waiter.exception(), ConnectionResetError) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-14 16:56:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main() |