Python twisted.protocols.policies.TrafficLoggingFactory() Examples

The following are 7 code examples of twisted.protocols.policies.TrafficLoggingFactory(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.protocols.policies , or try the search function .
Example #1
Source File: test_policies.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_thingsGetLogged(self):
        """
        Check the output produced by L{policies.TrafficLoggingFactory}.
        """
        wrappedFactory = Server()
        wrappedFactory.protocol = WriteSequenceEchoProtocol
        t = StringTransportWithDisconnection()
        f = TestLoggingFactory(wrappedFactory, 'test')
        p = f.buildProtocol(('1.2.3.4', 5678))
        t.protocol = p
        p.makeConnection(t)

        v = f.openFile.getvalue()
        self.assertIn('*', v)
        self.assertFalse(t.value())

        p.dataReceived(b'here are some bytes')

        v = f.openFile.getvalue()
        self.assertIn("C 1: %r" % (b'here are some bytes',), v)
        self.assertIn("S 1: %r" % (b'here are some bytes',), v)
        self.assertEqual(t.value(), b'here are some bytes')

        t.clear()
        p.dataReceived(b'prepare for vector! to the extreme')
        v = f.openFile.getvalue()
        self.assertIn("SV 1: %r" % ([b'prepare for vector! to the extreme'],), v)
        self.assertEqual(t.value(), b'prepare for vector! to the extreme')

        p.loseConnection()

        v = f.openFile.getvalue()
        self.assertIn('ConnectionDone', v) 
Example #2
Source File: test_policies.py    From Safejumper-for-Desktop with GNU General Public License v2.0 5 votes vote down vote up
def test_loggingFactoryOpensLogfileAutomatically(self):
        """
        When the L{policies.TrafficLoggingFactory} builds a protocol, it
        automatically opens a unique log file for that protocol and attaches
        the logfile to the built protocol.
        """
        open_calls = []
        open_rvalues = []

        def mocked_open(*args, **kwargs):
            """
            Mock for the open call to prevent actually opening a log file.
            """
            open_calls.append((args, kwargs))
            io = NativeStringIO()
            io.name = args[0]
            open_rvalues.append(io)
            return io

        self.patch(builtins, 'open', mocked_open)

        wrappedFactory = protocol.ServerFactory()
        wrappedFactory.protocol = SimpleProtocol
        factory = policies.TrafficLoggingFactory(wrappedFactory, 'test')
        first_proto = factory.buildProtocol(address.IPv4Address('TCP',
                                                                '127.0.0.1',
                                                                12345))
        second_proto = factory.buildProtocol(address.IPv4Address('TCP',
                                                                 '127.0.0.1',
                                                                 12346))

        # We expect open to be called twice, with the files passed to the
        # protocols.
        first_call = (('test-1', 'w'), {})
        second_call = (('test-2', 'w'), {})
        self.assertEqual([first_call, second_call], open_calls)
        self.assertEqual(
            [first_proto.logfile, second_proto.logfile], open_rvalues
        ) 
Example #3
Source File: test_policies.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_thingsGetLogged(self):
        """
        Check the output produced by L{policies.TrafficLoggingFactory}.
        """
        wrappedFactory = Server()
        wrappedFactory.protocol = WriteSequenceEchoProtocol
        t = StringTransportWithDisconnection()
        f = TestLoggingFactory(wrappedFactory, 'test')
        p = f.buildProtocol(('1.2.3.4', 5678))
        t.protocol = p
        p.makeConnection(t)

        v = f.openFile.getvalue()
        self.assertIn('*', v)
        self.assertFalse(t.value())

        p.dataReceived(b'here are some bytes')

        v = f.openFile.getvalue()
        self.assertIn("C 1: %r" % (b'here are some bytes',), v)
        self.assertIn("S 1: %r" % (b'here are some bytes',), v)
        self.assertEqual(t.value(), b'here are some bytes')

        t.clear()
        p.dataReceived(b'prepare for vector! to the extreme')
        v = f.openFile.getvalue()
        self.assertIn("SV 1: %r" % ([b'prepare for vector! to the extreme'],), v)
        self.assertEqual(t.value(), b'prepare for vector! to the extreme')

        p.loseConnection()

        v = f.openFile.getvalue()
        self.assertIn('ConnectionDone', v) 
Example #4
Source File: test_policies.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def test_loggingFactoryOpensLogfileAutomatically(self):
        """
        When the L{policies.TrafficLoggingFactory} builds a protocol, it
        automatically opens a unique log file for that protocol and attaches
        the logfile to the built protocol.
        """
        open_calls = []
        open_rvalues = []

        def mocked_open(*args, **kwargs):
            """
            Mock for the open call to prevent actually opening a log file.
            """
            open_calls.append((args, kwargs))
            io = NativeStringIO()
            io.name = args[0]
            open_rvalues.append(io)
            return io

        self.patch(builtins, 'open', mocked_open)

        wrappedFactory = protocol.ServerFactory()
        wrappedFactory.protocol = SimpleProtocol
        factory = policies.TrafficLoggingFactory(wrappedFactory, 'test')
        first_proto = factory.buildProtocol(address.IPv4Address('TCP',
                                                                '127.0.0.1',
                                                                12345))
        second_proto = factory.buildProtocol(address.IPv4Address('TCP',
                                                                 '127.0.0.1',
                                                                 12346))

        # We expect open to be called twice, with the files passed to the
        # protocols.
        first_call = (('test-1', 'w'), {})
        second_call = (('test-2', 'w'), {})
        self.assertEqual([first_call, second_call], open_calls)
        self.assertEqual(
            [first_proto.logfile, second_proto.logfile], open_rvalues
        ) 
Example #5
Source File: test_core.py    From powerstrip with Apache License 2.0 5 votes vote down vote up
def _getAdder(self, *args, **kw):
        self.adderAPI = TrafficLoggingFactory(AdderPlugin(*args, **kw), "adder-")
        self.adderServer = reactor.listenTCP(0, self.adderAPI)
        self.adderPort = self.adderServer.getHost().port 
Example #6
Source File: test_core.py    From powerstrip with Apache License 2.0 5 votes vote down vote up
def _getAdderTwo(self, *args, **kw):
        kw["incrementBy"] = 2
        self.adderTwoAPI = TrafficLoggingFactory(AdderPlugin(*args, **kw), "adder2-")
        self.adderTwoServer = reactor.listenTCP(0, self.adderTwoAPI)
        self.adderTwoPort = self.adderTwoServer.getHost().port 
Example #7
Source File: test_policies.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_thingsGetLogged(self):
        """
        Check the output produced by L{policies.TrafficLoggingFactory}.
        """
        wrappedFactory = Server()
        wrappedFactory.protocol = WriteSequenceEchoProtocol
        t = StringTransportWithDisconnection()
        f = TestLoggingFactory(wrappedFactory, 'test')
        p = f.buildProtocol(('1.2.3.4', 5678))
        t.protocol = p
        p.makeConnection(t)

        v = f.openFile.getvalue()
        self.failUnless('*' in v, "* not found in %r" % (v,))
        self.failIf(t.value())

        p.dataReceived('here are some bytes')

        v = f.openFile.getvalue()
        self.assertIn("C 1: 'here are some bytes'", v)
        self.assertIn("S 1: 'here are some bytes'", v)
        self.assertEquals(t.value(), 'here are some bytes')

        t.clear()
        p.dataReceived('prepare for vector! to the extreme')
        v = f.openFile.getvalue()
        self.assertIn("SV 1: ['prepare for vector! to the extreme']", v)
        self.assertEquals(t.value(), 'prepare for vector! to the extreme')

        p.loseConnection()

        v = f.openFile.getvalue()
        self.assertIn('ConnectionDone', v)