'POP3Client.startTLS starts a TLS session over its existing TCP
connection.'

def test_startTLS(self):

    sf = TLSServerFactory()
    sf.protocol.output = [['+OK'], ['+OK', 'STLS', '.'], ['+OK'], ['+OK', '.'], ['+OK']]
    sf.protocol.context = ServerTLSContext()
    port = reactor.listenTCP(0, sf, interface='127.0.0.1')
    self.addCleanup(port.stopListening)
    H = port.getHost().host
    P = port.getHost().port
    connLostDeferred = defer.Deferred()
    cp = SimpleClient(defer.Deferred(), ClientTLSContext())
    def connectionLost(reason):
        SimpleClient.connectionLost(cp, reason)
        connLostDeferred.callback(None)
    cp.connectionLost = connectionLost
    cf = protocol.ClientFactory()
    cf.protocol = (lambda : cp)
    conn = reactor.connectTCP(H, P, cf)
    def cbConnected(ignored):
        log.msg('Connected    to    server;    starting    TLS')
        return cp.startTLS()
    def cbStartedTLS(ignored):
        log.msg('Started    TLS;    disconnecting')
        return cp.quit()
    def cbDisconnected(ign):
        log.msg('Disconnected;    asserting    correct    input    received')
        self.assertEqual(sf.input, ['CAPA', 'STLS', 'CAPA', 'QUIT'])
    def cleanup(result):
        log.msg('Asserted    correct    input;    disconnecting    client    and    shutting    down    server')
        conn.disconnect()
        return connLostDeferred
    cp.deferred.addCallback(cbConnected)
    cp.deferred.addCallback(cbStartedTLS)
    cp.deferred.addCallback(cbDisconnected)
    cp.deferred.addBoth(cleanup)
    return cp.deferred
