Ticket #6556: fix-max-length.2.patch

File fix-max-length.2.patch, 3.6 KB (added by zooko, 8 years ago)
  • twisted/protocols/test/test_basic.py

     
    149149        self.received.append(line)
    150150
    151151
     152class BasicLineTester(basic.LineReceiver):
     153    """
     154    A line receiver that stores received lines in self.received.
     155    """
     156    def connectionMade(self):
     157        """
     158        Create/clean data received on connection.
     159        """
     160        self.received = []
     161
     162    def lineReceived(self, line):
     163        """
     164        Receive line and store it.
     165        """
     166        self.received.append(line)
     167 
     168
    152169class LineReceiverTestCase(unittest.SynchronousTestCase):
    153170    """
    154171    Test L{twisted.protocols.basic.LineReceiver}, using the C{LineTester}
     
    314331        self.assertTrue(transport.disconnecting)
    315332
    316333
    317     def test_maximumLineLengthRemaining(self):
     334    def test_maximumLineLengthPartialDelimiter(self):
    318335        """
    319         C{LineReceiver} disconnects the transport it if receives a non-finished
    320         line longer than its C{MAX_LENGTH}.
     336        C{LineReceiver} doesn't disconnect the transport when it
     337        receives a finished line as long as its C{MAX_LENGTH}, when
     338        the second-to-last packet ended with a pattern that could have
     339        been -- and turns out to have been -- the start of a
     340        delimiter, and that packet causes the total input to exceed
     341        C{MAX_LENGTH} + len(delimiter).
    321342        """
     343        proto = BasicLineTester()
     344        proto.MAX_LENGTH = 4
     345        t = proto_helpers.StringTransport()
     346        proto.makeConnection(t)
     347
     348        line = b'x' * (proto.MAX_LENGTH - 1)
     349        proto.dataReceived(line)
     350        proto.dataReceived(proto.delimiter[:-1])
     351        proto.dataReceived(proto.delimiter[-1:] + line)
     352        self.assertFalse(t.disconnecting)
     353        self.assertEqual(line, proto.received and proto.received[0])
     354
     355
     356    def test_notQuiteMaximumLineLengthUnfinished(self):
     357        """
     358        C{LineReceiver} doesn't disconnect the transport it if
     359        receives a non-finished line whose length, counting the
     360        delimiter, is longer than its C{MAX_LENGTH} but shorter than
     361        its C{MAX_LENGTH} + len(delimiter). (When the first part that
     362        exceeds the max is the beginning of the delimiter.)
     363        """
    322364        proto = basic.LineReceiver()
     365        # '\r\n' is the default, but we set it just to be explicit in this test.
     366        proto.delimiter = '\r\n'
    323367        transport = proto_helpers.StringTransport()
    324368        proto.makeConnection(transport)
    325         proto.dataReceived(b'x' * (proto.MAX_LENGTH + 1))
    326         self.assertTrue(transport.disconnecting)
     369        proto.dataReceived((b'x' * proto.MAX_LENGTH) + proto.delimiter[:len(proto.delimiter)-1])
     370        self.assertFalse(transport.disconnecting)
    327371
    328372
    329373    def test_rawDataError(self):
  • twisted/protocols/basic.py

     
    558558                        line, self._buffer = self._buffer.split(
    559559                            self.delimiter, 1)
    560560                    except ValueError:
    561                         if len(self._buffer) > self.MAX_LENGTH:
     561                        if len(self._buffer) >= (self.MAX_LENGTH + len(self.delimiter)):
    562562                            line, self._buffer = self._buffer, b''
    563563                            return self.lineLengthExceeded(line)
    564564                        return