'Run test where client sends data before handshake, given the sending
protocol and expected bytes.'

def writeBeforeHandshakeTest(self, sendingProtocol, data):

    clientFactory = ClientFactory()
    clientFactory.protocol = sendingProtocol
    (clientContextFactory, handshakeDeferred) = HandshakeCallbackContextFactory.factoryAndDeferred()
    wrapperFactory = TLSMemoryBIOFactory(clientContextFactory, True, clientFactory)
    sslClientProtocol = wrapperFactory.buildProtocol(None)
    serverProtocol = AccumulatingProtocol(len(data))
    serverFactory = ServerFactory()
    serverFactory.protocol = (lambda : serverProtocol)
    serverContextFactory = ServerTLSContext()
    wrapperFactory = TLSMemoryBIOFactory(serverContextFactory, False, serverFactory)
    sslServerProtocol = wrapperFactory.buildProtocol(None)
    connectionDeferred = loopbackAsync(sslServerProtocol, sslClientProtocol)
    def cbConnectionDone(ignored):
        self.assertEqual(''.join(serverProtocol.received), data)
    connectionDeferred.addCallback(cbConnectionDone)
    return connectionDeferred
