Esempio n. 1
0
 def setUp(self):
     """
     Create a protocol instance linked with a L{DummyPortMapperFactory}.
     """
     self.proto = PortMapperProtocol()
     self.proto.factory = DummyPortMapperFactory()
     self.transport = StringTransportWithDisconnection()
     self.proto.makeConnection(self.transport)
     self.transport.protocol = self.proto
Esempio n. 2
0
class PortMapperProtocolTestCase(TestCase):
    """
    Tests for L{PortMapperProtocol}.
    """

    def setUp(self):
        """
        Create a protocol instance linked with a L{DummyPortMapperFactory}.
        """
        self.proto = PortMapperProtocol()
        self.proto.factory = DummyPortMapperFactory()
        self.transport = StringTransportWithDisconnection()
        self.proto.makeConnection(self.transport)
        self.transport.protocol = self.proto


    def test_alive2Response(self):
        """
        Test an successful alive2 response.
        """
        def cb(res):
            self.assertEqual(res, 1)
        self.proto.factory._connectDeferred.addCallback(cb)
        self.proto.dataReceived("y")
        self.proto.dataReceived("\x00")
        self.proto.dataReceived("\x00\x01")
        return self.proto.factory._connectDeferred


    def test_alive2ResponseError(self):
        """
        Test an erroneous alive2 response.
        """
        def eb(res):
            res.trap(ValueError)
        self.proto.factory._connectDeferred.addErrback(eb)
        self.proto.dataReceived("y\x01\x00\x01")
        return self.proto.factory._connectDeferred


    def test_alive2Request(self):
        """
        Test data sent by an alive2 request.
        """
        self.proto.alive2Request(1234, 77, (1, 2), "foo@bar")
        self.assertEqual(
            self.transport.value(),
            "\x00\x14x\x04\xd2M\x00\x00\x01\x00\x02\x00\x07foo@bar\x00\x00")


    def test_port2Response(self):
        """
        Test an successful port2 response.
        """
        d = Deferred()
        d.addCallback(self.assertEqual, Node(9, 77, 1, (5, 5), "bar", ""))
        self.proto.deferred = d
        self.proto.dataReceived("w")
        self.proto.dataReceived("\x00")
        self.proto.dataReceived("\x00\x09M\x01\x00\x05\x00\x05\x00\x03bar\x00")
        self.assertEqual(self.proto.received, "")
        return d


    def test_port2ResponseWithExtra(self):
        """
        Test an successful port2 response, with extra data.
        """
        d = Deferred()
        d.addCallback(
            self.assertEqual, Node(9, 77, 1, (5, 5), "bar", "spam"))
        self.proto.deferred = d
        self.proto.dataReceived(
            "w\x00\x00\x09M\x01\x00\x05\x00\x05\x00\x03bar\x00\x04spam")
        self.assertEqual(self.proto.received, "")
        return d


    def test_port2ResponseNodeNotFound(self):
        """
        Test a port2 not found response: the response deferred should be fired
        with a L{NodeNotFound} exception.
        """
        d = Deferred()
        self.proto.deferred = d
        self.proto.dataReceived("w\x01")
        self.assertEqual(self.proto.received, "")
        return self.assertFailure(d, NodeNotFound)


    def test_port2Request(self):
        """
        Test data sent by a port2 request.
        """
        d = Deferred()
        self.proto.portPlease2Request(d, "egg@spam")
        self.assertEqual(self.transport.value(), "\x00\tzegg@spam")
        self.assertEqual(self.proto.deferred, d)


    def test_names(self):
        """
        Test successful names request and response.
        """
        d = Deferred()
        d.addCallback(self.assertEqual, [("foo", 1234), ("egg", 4321)])
        self.proto.namesRequest(d)
        self.assertEqual(self.transport.value(), "\x00\x01n")
        self.proto.dataReceived("\x00\x00\x00\x01")
        self.proto.dataReceived("name %s at port %s\n" % ("foo", 1234))
        self.proto.dataReceived("name %s at port %s\n" % ("egg", 4321))
        self.transport.loseConnection()
        return d


    def test_dump(self):
        """
        Test successful dump request and response.
        """
        d = Deferred()
        d.addCallback(
            self.assertEqual, {"active": [("foo", 1234, 3)],
                               "old": [("egg", 4321, 2)]})
        self.proto.dumpRequest(d)
        self.assertEqual(self.transport.value(), "\x00\x01d")
        self.proto.dataReceived("\x00\x00\x00\x01")
        self.proto.dataReceived(
            "active name    <%s> at port %s, fd = %s\n\x00" % ("foo", 1234, 3))
        self.proto.dataReceived(
            "old/unused name, <%s>, at port %s, fd = %s\n\x00" % ("egg",
                                                                  4321, 2))
        self.transport.loseConnection()
        return d


    def test_kill(self):
        """
        Test successful kill request and response.
        """
        d = Deferred()
        d.addCallback(self.assertEqual, "OK")
        self.proto.killRequest(d)
        self.assertEqual(self.transport.value(), "\x00\x01k")
        self.proto.dataReceived("OK")
        self.transport.loseConnection()
        return d


    def test_invalidKillResponse(self):
        """
        Test a kill request with an invalid response.
        """
        d = Deferred()
        self.proto.killRequest(d)
        self.assertEqual(self.transport.value(), "\x00\x01k")
        self.proto.dataReceived("Wrong")
        self.transport.loseConnection()
        return self.assertFailure(d, ValueError)


    def test_connectionLostWithPendingDeferred(self):
        """
        Test that connectionLost errbacks the action deferred if present.
        """
        d = Deferred()
        self.proto.deferred = d
        self.transport.loseConnection()
        return self.assertFailure(d, ConnectionDone)


    def test_unhandledRequest(self):
        """
        When a unhandled data is received, the protocol should log a
        C{RuntimeError}.
        """
        self.proto.dataReceived("Wrong")
        errors = self.flushLoggedErrors()
        self.assertEqual(len(errors), 1)
        errors[0].trap(RuntimeError)