def buildTLSProtocol(server=False, transport=None, fakeConnection=None):
    """
    Create a protocol hooked up to a TLS transport hooked up to a
    StringTransport.
    """
    # We want to accumulate bytes without disconnecting, so set high limit:
    clientProtocol = AccumulatingProtocol(999999999999)
    clientFactory = ClientFactory()
    clientFactory.protocol = lambda: clientProtocol

    if fakeConnection:
        @implementer(IOpenSSLServerConnectionCreator,
                     IOpenSSLClientConnectionCreator)
        class HardCodedConnection(object):
            def clientConnectionForTLS(self, tlsProtocol):
                return fakeConnection
            serverConnectionForTLS = clientConnectionForTLS
        contextFactory = HardCodedConnection()
    else:
        if server:
            contextFactory = ServerTLSContext()
        else:
            contextFactory = ClientTLSContext()
    wrapperFactory = TLSMemoryBIOFactory(
        contextFactory, not server, clientFactory)
    sslProtocol = wrapperFactory.buildProtocol(None)

    if transport is None:
        transport = StringTransport()
    sslProtocol.makeConnection(transport)
    return clientProtocol, sslProtocol

    Create a protocol hooked up to a TLS transport hooked up to a
    StringTransport.
    
stackoverflow/venv/lib/python3.6/site-packages/twisted/protocols/test/test_tls.py