def test_startTLS(self):
        """
        POP3Client.startTLS starts a TLS session over its existing TCP
        connection.
        """
        sf = TLSServerFactory()
        sf.protocol.output = [
            ['+OK'],  # Server greeting
            ['+OK', 'STLS', '.'],  # CAPA response
            ['+OK'],  # STLS response
            ['+OK', '.'],  # Second CAPA response
            ['+OK']  # QUIT response
        ]
        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
Example #2
0
    def startTLS(self):
        if self.ctx is None:
            self.ctx = ServerTLSContext(CERT_FILE)

        if SSL_SUPPORT and self.ctx is not None:
            self.sendLine(BEGIN_TLS)
            self.transport.startTLS(self.ctx)
        else:
            self.sendLine(TLS_NOT_AVAILABLE)
Example #3
0
    def testTLS(self):
        clientCTX = ClientTLSContext()
        serverCTX = ServerTLSContext()

        client = NoticeTLSClient(contextFactory=clientCTX)
        server = DummyESMTP(contextFactory=serverCTX)

        def check(ignored):
            self.assertEquals(client.tls, True)
            self.assertEquals(server.startedTLS, True)

        return self.loopback(server, client).addCallback(check)
    def testStartTLS(self):
        sf = TLSServerFactory()
        sf.protocol.output = [
            ['+OK'],  # Server greeting
            ['+OK', 'STLS', '.'],  # CAPA response
            ['+OK'],  # STLS response
            ['+OK', '.'],  # Second CAPA response
            ['+OK']  # QUIT response
        ]
        sf.protocol.context = ServerTLSContext()
        port = reactor.listenTCP(0, sf, interface='127.0.0.1')
        H = port.getHost().host
        P = port.getHost().port

        cp = SimpleClient(defer.Deferred(), ClientTLSContext())
        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.assertEquals(sf.input, ['CAPA', 'STLS', 'CAPA', 'QUIT'])

        def cleanup(result):
            log.msg(
                "Asserted correct input; disconnecting client and shutting down server"
            )
            conn.disconnect()

            def cbShutdown(ignored):
                log.msg("Shut down server")
                return result

            return defer.maybeDeferred(
                port.stopListening).addCallback(cbShutdown)

        cp.deferred.addCallback(cbConnected)
        cp.deferred.addCallback(cbStartedTLS)
        cp.deferred.addCallback(cbDisconnected)
        cp.deferred.addBoth(cleanup)

        return cp.deferred
Example #5
0
def main():

    if len(sys.argv) < 2:
        printMessage("POP3 with no messages")
    else:
        args = sys.argv[1:]

        for arg in args:
            processArg(arg)

    f = Factory()
    f.protocol = POP3TestServer

    if START_SSL:
        reactor.listenSSL(SSL_PORT, f, ServerTLSContext(CERT_FILE))
    else:
        reactor.listenTCP(PORT, f)

    reactor.run()