Ticket #6557: LineOnlyReceiver-disconnect-on-overlarge-line.patch

File LineOnlyReceiver-disconnect-on-overlarge-line.patch, 2.7 KB (added by zooko, 6 years ago)
  • twisted/protocols/basic.py

     
    481481        Called when the maximum line length has been reached.
    482482        Override if it needs to be dealt with in some special way.
    483483        """
    484         return error.ConnectionLost('Line length exceeded')
     484        return self.transport.loseConnection()
    485485
    486486
    487487
  • twisted/protocols/test/test_basic.py

     
    128128
    129129
    130130
    131 class LineOnlyTester(basic.LineOnlyReceiver):
    132     """
    133     A buffering line only receiver.
    134     """
    135     delimiter = b'\n'
    136     MAX_LENGTH = 64
    137 
    138     def connectionMade(self):
    139         """
    140         Create/clean data received on connection.
    141         """
    142         self.received = []
    143 
    144 
    145     def lineReceived(self, line):
    146         """
    147         Save received data.
    148         """
    149         self.received.append(line)
    150 
    151 
    152131class LineReceiverTestCase(unittest.SynchronousTestCase):
    153132    """
    154133    Test L{twisted.protocols.basic.LineReceiver}, using the C{LineTester}
     
    359338
    360339
    361340
     341class LineOnlyTester(basic.LineOnlyReceiver):
     342    """
     343    A buffering line only receiver.
     344    """
     345    delimiter = b'\n'
     346    MAX_LENGTH = 64
     347
     348    def connectionMade(self):
     349        """
     350        Create/clean data received on connection.
     351        """
     352        self.received = []
     353
     354
     355    def lineReceived(self, line):
     356        """
     357        Save received data.
     358        """
     359        self.received.append(line)
     360
     361
    362362class LineOnlyReceiverTestCase(unittest.SynchronousTestCase):
    363363    """
    364364    Tests for L{twisted.protocols.basic.LineOnlyReceiver}.
     
    381381        self.assertEqual(a.received, self.buffer.split(b'\n')[:-1])
    382382
    383383
    384     def test_lineTooLong(self):
     384    def test_greaterThanMaximumLineLength(self):
    385385        """
    386         Test sending a line too long: it should close the connection.
     386        C{LineOnlyReceiver} disconnects the transport if it receives a
     387        line longer than its C{MAX_LENGTH} + len(delimiter).
    387388        """
    388         t = proto_helpers.StringTransport()
    389         a = LineOnlyTester()
    390         a.makeConnection(t)
    391         res = a.dataReceived(b'x' * 200)
    392         self.assertIsInstance(res, error.ConnectionLost)
     389        proto = LineOnlyTester()
     390        transport = proto_helpers.StringTransport()
     391        proto.makeConnection(transport)
     392        proto.dataReceived(b'x' * (proto.MAX_LENGTH + len(proto.delimiter) + 1) + b'\r\nr')
     393        self.assertTrue(transport.disconnecting)
    393394
    394395
    395396    def test_lineReceivedNotImplemented(self):