'L{TLSMemoryBIOProtocol} reports errors in the handshake process to the
application-level protocol object using its C{connectionLost} method
and disconnects the underlying transport.'

def test_handshakeFailure(self):

    clientConnectionLost = Deferred()
    clientFactory = ClientFactory()
    clientFactory.protocol = (lambda : ConnectionLostNotifyingProtocol(clientConnectionLost))
    clientContextFactory = HandshakeCallbackContextFactory()
    wrapperFactory = TLSMemoryBIOFactory(clientContextFactory, True, clientFactory)
    sslClientProtocol = wrapperFactory.buildProtocol(None)
    serverConnectionLost = Deferred()
    serverFactory = ServerFactory()
    serverFactory.protocol = (lambda : ConnectionLostNotifyingProtocol(serverConnectionLost))
    certificateData = FilePath(certPath).getContent()
    certificate = PrivateCertificate.loadPEM(certificateData)
    serverContextFactory = certificate.options(certificate)
    wrapperFactory = TLSMemoryBIOFactory(serverContextFactory, False, serverFactory)
    sslServerProtocol = wrapperFactory.buildProtocol(None)
    connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol)
    def cbConnectionLost(protocol):
        protocol.lostConnectionReason.trap(Error)
    clientConnectionLost.addCallback(cbConnectionLost)
    serverConnectionLost.addCallback(cbConnectionLost)
    return gatherResults([clientConnectionLost, serverConnectionLost, connectionDeferred])
