def test_tcp_connection_refused(self): # Ensure that create_connection raises an exception when connecting to # a TCP port where there is no listener. server = create_server(StreamProtocol, ('localhost', 0)) addr = server.addresses[0] server.close() self.assertRaises(TransportError, create_connection, StreamProtocol, addr)
def test_ssl_no_handshake_on_connect(self): # Ensure that when create_connection(ssl=True) returns, that the SSL # handshake has completed. context = self.get_ssl_context() server = create_server(StreamProtocol, ('localhost', 0), ssl=context) addr = server.addresses[0] ssl_args = {'do_handshake_on_connect': False} ctrans, cproto = create_connection(StreamProtocol, addr, ssl=context, ssl_args=ssl_args) # The SSL handshake has not been established at this point. sslobj = ctrans.get_extra_info('ssl') self.assertIsNone(sslobj) # Now initiate the SSL handshake and allow it some time to complete ctrans.do_handshake() gruvi.sleep(0.1) # There should be a channel binding now. sslobj = ctrans.get_extra_info('ssl') self.assertIsNotNone(sslobj) sslcb = sslobj.get_channel_binding('tls-unique') self.assertGreater(len(sslcb), 0) strans, sproto = list(server.connections)[0] cproto.stream.write(b'foo\n') self.assertEqual(sproto.stream.readline(), b'foo\n') server.close() self.assertEqual(len(list(server.connections)), 0) self.assertEqual(cproto.stream.readline(), b'') ctrans.close()
def test_tcp_ssl_certificate_error(self): # Ensure that create_connection() raises a CertificateError if the # certificate doesn't validate. if not hasattr(ssl, 'create_default_context'): raise SkipTest('Certificate validation not supported') server = create_server(StreamProtocol, ('localhost', 0), **self.ssl_s_args) addr = server.addresses[0] ssl_args = self.ssl_c_args.copy() ssl_args['server_hostname'] = 'foobar' self.assertRaises(TransportError, create_connection, StreamProtocol, addr, **ssl_args) server.close()
def test_pipe(self): # Ensure that create_connection() and create_server() can be used to # connect to each other over a pipe. addr = self.pipename() server = create_server(StreamProtocol, addr) ctrans, cproto = create_connection(StreamProtocol, addr) gruvi.sleep(0.1) # allow Server to accept() strans, sproto = list(server.connections)[0] cproto.stream.write(b'foo\n') self.assertEqual(sproto.stream.readline(), b'foo\n') server.close() self.assertEqual(len(list(server.connections)), 0) self.assertEqual(cproto.stream.readline(), b'') ctrans.close()
def test_pipe_ssl(self): # Ensure that create_connection() and create_server() can be used to # connect to each other over a pipe using SSL. addr = self.pipename() server = create_server(StreamProtocol, addr, **self.ssl_s_args) ctrans, cproto = create_connection(StreamProtocol, addr, **self.ssl_cp_args) # No need to sleep here because create_connection waits for the SSL # handshake to complete strans, sproto = list(server.connections)[0] cproto.stream.write(b'foo\n') self.assertEqual(sproto.stream.readline(), b'foo\n') server.close() self.assertEqual(len(list(server.connections)), 0) self.assertEqual(cproto.stream.readline(), b'') ctrans.close()
def test_tcp_ssl(self): # Ensure that create_connection() and create_server() can be used to # connect to each other over TCP using SSL. context = self.get_ssl_context() server = create_server(StreamProtocol, ('localhost', 0), ssl=context) addr = server.addresses[0] ctrans, cproto = create_connection(StreamProtocol, addr, ssl=context) # No need to sleep here because create_connection waits for the SSL # handshake to complete strans, sproto = list(server.connections)[0] cproto.stream.write(b'foo\n') self.assertEqual(sproto.stream.readline(), b'foo\n') server.close() self.assertEqual(len(list(server.connections)), 0) self.assertEqual(cproto.stream.readline(), b'') ctrans.close()
def test_pipe_ssl(self): # Ensure that create_connection() and create_server() can be used to # connect to each other over a pipe using SSL. context = self.get_ssl_context() addr = self.pipename() server = create_server(StreamProtocol, addr, ssl=context) ctrans, cproto = create_connection(StreamProtocol, addr, ssl=context) # No need to sleep here because create_connection waits for the SSL # handshake to complete strans, sproto = list(server.connections)[0] cproto.stream.write(b'foo\n') self.assertEqual(sproto.stream.readline(), b'foo\n') server.close() self.assertEqual(len(list(server.connections)), 0) self.assertEqual(cproto.stream.readline(), b'') ctrans.close()
def test_ssl_handshake_on_connect(self): # Ensure that when create_connection(ssl=True) returns, that the SSL # handshake has completed. server = create_server(StreamProtocol, ('localhost', 0), **self.ssl_s_args) addr = server.addresses[0] ctrans, cproto = create_connection(StreamProtocol, addr, **self.ssl_c_args) # The SSL handshake should be completed at this point. This means that # there should be a channel binding. sslobj = ctrans.get_extra_info('ssl') self.assertIsNotNone(sslobj) sslcb = sslobj.get_channel_binding('tls-unique') self.assertGreater(len(sslcb), 0) strans, sproto = list(server.connections)[0] cproto.stream.write(b'foo\n') self.assertEqual(sproto.stream.readline(), b'foo\n') server.close() self.assertEqual(len(list(server.connections)), 0) self.assertEqual(cproto.stream.readline(), b'') ctrans.close()
def test_ssl_handshake_on_connect(self): # Ensure that when create_connection(ssl=True) returns, that the SSL # handshake has completed. context = self.get_ssl_context() server = create_server(StreamProtocol, ('localhost', 0), ssl=context) addr = server.addresses[0] ctrans, cproto = create_connection(StreamProtocol, addr, ssl=context) # The SSL handshake should be completed at this point. This means that # there should be a channel binding. sslobj = ctrans.get_extra_info('ssl') self.assertIsNotNone(sslobj) sslcb = sslobj.get_channel_binding('tls-unique') self.assertGreater(len(sslcb), 0) strans, sproto = list(server.connections)[0] cproto.stream.write(b'foo\n') self.assertEqual(sproto.stream.readline(), b'foo\n') server.close() self.assertEqual(len(list(server.connections)), 0) self.assertEqual(cproto.stream.readline(), b'') ctrans.close()