PHP 8.5.2
Preview: test_protocols.py Size: 7.16 KB
//lib/python3/dist-packages/twisted/test/test_protocols.py

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

"""
Test cases for twisted.protocols package.
"""

from twisted.internet import address, defer, protocol, reactor
from twisted.protocols import portforward, wire
from twisted.python.compat import iterbytes
from twisted.test import proto_helpers
from twisted.trial import unittest


class WireTests(unittest.TestCase):
    """
    Test wire protocols.
    """

    def test_echo(self):
        """
        Test wire.Echo protocol: send some data and check it send it back.
        """
        t = proto_helpers.StringTransport()
        a = wire.Echo()
        a.makeConnection(t)
        a.dataReceived(b"hello")
        a.dataReceived(b"world")
        a.dataReceived(b"how")
        a.dataReceived(b"are")
        a.dataReceived(b"you")
        self.assertEqual(t.value(), b"helloworldhowareyou")

    def test_who(self):
        """
        Test wire.Who protocol.
        """
        t = proto_helpers.StringTransport()
        a = wire.Who()
        a.makeConnection(t)
        self.assertEqual(t.value(), b"root\r\n")

    def test_QOTD(self):
        """
        Test wire.QOTD protocol.
        """
        t = proto_helpers.StringTransport()
        a = wire.QOTD()
        a.makeConnection(t)
        self.assertEqual(t.value(), b"An apple a day keeps the doctor away.\r\n")

    def test_discard(self):
        """
        Test wire.Discard protocol.
        """
        t = proto_helpers.StringTransport()
        a = wire.Discard()
        a.makeConnection(t)
        a.dataReceived(b"hello")
        a.dataReceived(b"world")
        a.dataReceived(b"how")
        a.dataReceived(b"are")
        a.dataReceived(b"you")
        self.assertEqual(t.value(), b"")


class TestableProxyClientFactory(portforward.ProxyClientFactory):
    """
    Test proxy client factory that keeps the last created protocol instance.

    @ivar protoInstance: the last instance of the protocol.
    @type protoInstance: L{portforward.ProxyClient}
    """

    def buildProtocol(self, addr):
        """
        Create the protocol instance and keeps track of it.
        """
        proto = portforward.ProxyClientFactory.buildProtocol(self, addr)
        self.protoInstance = proto
        return proto


class TestableProxyFactory(portforward.ProxyFactory):
    """
    Test proxy factory that keeps the last created protocol instance.

    @ivar protoInstance: the last instance of the protocol.
    @type protoInstance: L{portforward.ProxyServer}

    @ivar clientFactoryInstance: client factory used by C{protoInstance} to
        create forward connections.
    @type clientFactoryInstance: L{TestableProxyClientFactory}
    """

    def buildProtocol(self, addr):
        """
        Create the protocol instance, keeps track of it, and makes it use
        C{clientFactoryInstance} as client factory.
        """
        proto = portforward.ProxyFactory.buildProtocol(self, addr)
        self.clientFactoryInstance = TestableProxyClientFactory()
        # Force the use of this specific instance
        proto.clientProtocolFactory = lambda: self.clientFactoryInstance
        self.protoInstance = proto
        return proto


class PortforwardingTests(unittest.TestCase):
    """
    Test port forwarding.
    """

    def setUp(self):
        self.serverProtocol = wire.Echo()
        self.clientProtocol = protocol.Protocol()
        self.openPorts = []

    def tearDown(self):
        try:
            self.proxyServerFactory.protoInstance.transport.loseConnection()
        except AttributeError:
            pass
        try:
            pi = self.proxyServerFactory.clientFactoryInstance.protoInstance
            pi.transport.loseConnection()
        except AttributeError:
            pass
        try:
            self.clientProtocol.transport.loseConnection()
        except AttributeError:
            pass
        try:
            self.serverProtocol.transport.loseConnection()
        except AttributeError:
            pass
        return defer.gatherResults(
            [defer.maybeDeferred(p.stopListening) for p in self.openPorts]
        )

    def test_portforward(self):
        """
        Test port forwarding through Echo protocol.
        """
        realServerFactory = protocol.ServerFactory()
        realServerFactory.protocol = lambda: self.serverProtocol
        realServerPort = reactor.listenTCP(0, realServerFactory, interface="127.0.0.1")
        self.openPorts.append(realServerPort)
        self.proxyServerFactory = TestableProxyFactory(
            "127.0.0.1", realServerPort.getHost().port
        )
        proxyServerPort = reactor.listenTCP(
            0, self.proxyServerFactory, interface="127.0.0.1"
        )
        self.openPorts.append(proxyServerPort)

        nBytes = 1000
        received = []
        d = defer.Deferred()

        def testDataReceived(data):
            received.extend(iterbytes(data))
            if len(received) >= nBytes:
                self.assertEqual(b"".join(received), b"x" * nBytes)
                d.callback(None)

        self.clientProtocol.dataReceived = testDataReceived

        def testConnectionMade():
            self.clientProtocol.transport.write(b"x" * nBytes)

        self.clientProtocol.connectionMade = testConnectionMade

        clientFactory = protocol.ClientFactory()
        clientFactory.protocol = lambda: self.clientProtocol

        reactor.connectTCP("127.0.0.1", proxyServerPort.getHost().port, clientFactory)

        return d

    def test_registerProducers(self):
        """
        The proxy client registers itself as a producer of the proxy server and
        vice versa.
        """
        # create a ProxyServer instance
        addr = address.IPv4Address("TCP", "127.0.0.1", 0)
        server = portforward.ProxyFactory("127.0.0.1", 0).buildProtocol(addr)

        # set the reactor for this test
        reactor = proto_helpers.MemoryReactor()
        server.reactor = reactor

        # make the connection
        serverTransport = proto_helpers.StringTransport()
        server.makeConnection(serverTransport)

        # check that the ProxyClientFactory is connecting to the backend
        self.assertEqual(len(reactor.tcpClients), 1)
        # get the factory instance and check it's the one we expect
        host, port, clientFactory, timeout, _ = reactor.tcpClients[0]
        self.assertIsInstance(clientFactory, portforward.ProxyClientFactory)

        # Connect it
        client = clientFactory.buildProtocol(addr)
        clientTransport = proto_helpers.StringTransport()
        client.makeConnection(clientTransport)

        # check that the producers are registered
        self.assertIs(clientTransport.producer, serverTransport)
        self.assertIs(serverTransport.producer, clientTransport)
        # check the streaming attribute in both transports
        self.assertTrue(clientTransport.streaming)
        self.assertTrue(serverTransport.streaming)


class StringTransportTests(unittest.TestCase):
    """
    Test L{proto_helpers.StringTransport} helper behaviour.
    """

    def test_noUnicode(self):
        """
        Test that L{proto_helpers.StringTransport} doesn't accept unicode data.
        """
        s = proto_helpers.StringTransport()
        self.assertRaises(TypeError, s.write, "foo")

Directory Contents

Dirs: 1 × Files: 104

Name Size Perms Modified Actions
- drwxr-xr-x 2026-01-08 12:56:24
Edit Download
1.46 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
654 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
34.88 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
18.40 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.63 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.25 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
172 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
172 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
925 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
400 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
566 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
123 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
214 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
983 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
233 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
268 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
297 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
178 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
220 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
739 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
787 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
130 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.15 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.34 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
60 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
81 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
48 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.23 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.72 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.14 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.00 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.06 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.13 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.55 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.45 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
902 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
894 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.06 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.66 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
25.47 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
108.04 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
33.34 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
17.75 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.43 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
20.84 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
143.37 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
13.01 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.90 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.60 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.46 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.28 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.89 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.27 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
126.96 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.65 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.19 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.56 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
45.38 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
9.58 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
13.31 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
15.54 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
36.86 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
17.79 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
13.99 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
2.12 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
24.69 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
17.84 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.38 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
73.64 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
12.23 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
14.73 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
26.02 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
32.28 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
4.32 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
86.29 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.16 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.63 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
7.40 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
23.89 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.65 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.91 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
24.90 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.53 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
17.09 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
22.73 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
113.84 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.97 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
12.43 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
5.10 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.67 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
47.73 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
64.26 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
12.73 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.47 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
3.26 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
21.64 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
12.90 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
1.69 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
72.29 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
6.13 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
26.79 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
13.26 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
22.76 KB lrw-r--r-- 2024-08-27 10:30:39
Edit Download
475 B lrw-r--r-- 2024-08-27 10:30:39
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).