def handshakingClientAndServer(clientGreetingData=None,
                               clientAbortAfterHandshake=False):
    """
    Construct a client and server L{TLSMemoryBIOProtocol} connected by an IO
    pump.

    @param greetingData: The data which should be written in L{connectionMade}.
    @type greetingData: L{bytes}

    @return: 3-tuple of client, server, L{twisted.test.iosim.IOPump}
    """
    authCert, serverCert = certificatesForAuthorityAndServer()
    @implementer(IHandshakeListener)
    class Client(AccumulatingProtocol, object):
        handshook = False
        peerAfterHandshake = None

        def connectionMade(self):
            super(Client, self).connectionMade()
            if clientGreetingData is not None:
                self.transport.write(clientGreetingData)

        def handshakeCompleted(self):
            self.handshook = True
            self.peerAfterHandshake = self.transport.getPeerCertificate()
            if clientAbortAfterHandshake:
                self.transport.abortConnection()

        def connectionLost(self, reason):
            pass

    @implementer(IHandshakeListener)
    class Server(AccumulatingProtocol, object):
        handshaked = False
        def handshakeCompleted(self):
            self.handshaked = True

        def connectionLost(self, reason):
            pass

    clientF = TLSMemoryBIOFactory(
        optionsForClientTLS(u"example.com", trustRoot=authCert),
        isClient=True,
        wrappedFactory=ClientFactory.forProtocol(lambda: Client(999999))
    )
    serverF = TLSMemoryBIOFactory(
        serverCert.options(), isClient=False,
        wrappedFactory=ServerFactory.forProtocol(lambda: Server(999999))
    )
    client, server, pump = connectedServerAndClient(
        lambda: serverF.buildProtocol(None),
        lambda: clientF.buildProtocol(None),
        greet=False,
    )
    return client, server, pump

    Construct a client and server L{TLSMemoryBIOProtocol} connected by an IO
    pump.

    @param greetingData: The data which should be written in L{connectionMade}.
    @type greetingData: L{bytes}

    @return: 3-tuple of client, server, L{twisted.test.iosim.IOPump}
    
stackoverflow/venv/lib/python3.6/site-packages/twisted/protocols/test/test_tls.py