Exemplo n.º 1
0
 def testReadBytes(self):
   s = 'abcd'
   # Also test going past the total stream length.
   for i in range(len(s) + 10):
     stream = input_stream.InputStream(s)
     self.assertEqual(s[:i], stream.ReadBytes(i))
     self.assertEqual(min(i, len(s)), stream.Position())
   stream = input_stream.InputStream(s)
   self.assertRaises(message.DecodeError, stream.ReadBytes, -1)
Exemplo n.º 2
0
  def testReadVarUInt64Failure(self):
    self.EnsureFailureOnEmptyStream(input_stream.InputStream.ReadVarUInt64)
    # Try and fail to read something with the mythical 64th bit set.
    s = '\x80\x80\x80\x80\x80\x80\x80\x80\x80\x02'
    stream = input_stream.InputStream(s)
    self.assertRaises(message.DecodeError, stream.ReadVarUInt64)

    # Try and fail to read something that looks like
    # a varint with more than 10 bytes.
    s = '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00'
    stream = input_stream.InputStream(s)
    self.assertRaises(message.DecodeError, stream.ReadVarUInt64)
Exemplo n.º 3
0
  def testReadVarUInt32Failure(self):
    self.EnsureFailureOnEmptyStream(input_stream.InputStream.ReadVarUInt32)
    # Try and fail to read UINT32_MAX + 1
    s = '\x80\x80\x80\x80\x10'
    stream = input_stream.InputStream(s)
    self.assertRaises(message.DecodeError, stream.ReadVarUInt32)

    # Try and fail to read something that looks like
    # a varint with more than 10 bytes.
    s = '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00'
    stream = input_stream.InputStream(s)
    self.assertRaises(message.DecodeError, stream.ReadVarUInt32)
Exemplo n.º 4
0
 def EnsureFailureOnEmptyStream(self, input_stream_method):
   """Helper for integer-parsing tests below.
   Ensures that the given InputStream method raises a DecodeError
   if called on a stream with no bytes remaining.
   """
   stream = input_stream.InputStream('')
   self.assertRaises(message.DecodeError, input_stream_method, stream)
Exemplo n.º 5
0
    def __init__(self, s):
        """Initializes the decoder to read from s.

    Args:
      s: An immutable sequence of bytes, which must be accessible
        via the Python buffer() primitive (i.e., buffer(s)).
    """
        self._stream = input_stream.InputStream(s)
Exemplo n.º 6
0
 def testPosition(self):
   stream = input_stream.InputStream('abcd')
   self.assertEqual(0, stream.Position())
   self.assertEqual(0, stream.Position())  # No side-effects.
   stream.ReadBytes(1)
   self.assertEqual(1, stream.Position())
   stream.ReadBytes(1)
   self.assertEqual(2, stream.Position())
   stream.ReadBytes(10)
   self.assertEqual(4, stream.Position())  # Can't go past end of stream.
Exemplo n.º 7
0
  def testSkipBytes(self):
    stream = input_stream.InputStream('')
    # Skipping bytes when at the end of stream
    # should have no effect.
    stream.SkipBytes(0)
    stream.SkipBytes(1)
    stream.SkipBytes(2)
    self.assertTrue(stream.EndOfStream())
    self.assertEqual(0, stream.Position())

    # Try skipping within a stream.
    stream = input_stream.InputStream('abcd')
    self.assertEqual(0, stream.Position())
    stream.SkipBytes(1)
    self.assertEqual(1, stream.Position())
    stream.SkipBytes(10)  # Can't skip past the end.
    self.assertEqual(4, stream.Position())

    # Ensure that a negative skip raises an exception.
    stream = input_stream.InputStream('abcd')
    stream.SkipBytes(1)
    self.assertRaises(message.DecodeError, stream.SkipBytes, -1)
Exemplo n.º 8
0
 def testGetSubBuffer(self):
   stream = input_stream.InputStream('abcd')
   # Try leaving out the size.
   self.assertEqual('abcd', str(stream.GetSubBuffer()))
   stream.SkipBytes(1)
   # GetSubBuffer() always starts at current size.
   self.assertEqual('bcd', str(stream.GetSubBuffer()))
   # Try 0-size.
   self.assertEqual('', str(stream.GetSubBuffer(0)))
   # Negative sizes should raise an error.
   self.assertRaises(message.DecodeError, stream.GetSubBuffer, -1)
   # Positive sizes should work as expected.
   self.assertEqual('b', str(stream.GetSubBuffer(1)))
   self.assertEqual('bc', str(stream.GetSubBuffer(2)))
   # Sizes longer than remaining bytes in the buffer should
   # return the whole remaining buffer.
   self.assertEqual('bcd', str(stream.GetSubBuffer(1000)))
Exemplo n.º 9
0
  def ReadVarintSuccessTestHelper(self, varints_and_ints, read_method):
    """Helper for tests below that test successful reads of various varints.

    Args:
      varints_and_ints: Iterable of (str, integer) pairs, where the string
        gives the wire encoding and the integer gives the value we expect
        to be returned by the read_method upon encountering this string.
      read_method: Unbound InputStream method that is capable of reading
        the encoded strings provided in the first elements of varints_and_ints.
    """
    s = ''.join(s for s, i in varints_and_ints)
    stream = input_stream.InputStream(s)
    expected_pos = 0
    self.assertEqual(expected_pos, stream.Position())
    for s, expected_int in varints_and_ints:
      self.assertEqual(expected_int, read_method(stream))
      expected_pos += len(s)
      self.assertEqual(expected_pos, stream.Position())
Exemplo n.º 10
0
 def testReadLittleEndian64(self):
   self.EnsureFailureOnEmptyStream(input_stream.InputStream.ReadLittleEndian64)
   s = ''
   # Read 0.
   s += '\x00\x00\x00\x00\x00\x00\x00\x00'
   # Read 1.
   s += '\x01\x00\x00\x00\x00\x00\x00\x00'
   # Read a bunch of different bytes.
   s += '\x01\x02\x03\x04\x05\x06\x07\x08'
   # Read max unsigned 64-bit int.
   s += '\xff\xff\xff\xff\xff\xff\xff\xff'
   # Try a read with fewer than 8 bytes left in the stream.
   s += '\x00\x00\x00'
   stream = input_stream.InputStream(s)
   self.assertEqual(0, stream.ReadLittleEndian64())
   self.assertEqual(8, stream.Position())
   self.assertEqual(1, stream.ReadLittleEndian64())
   self.assertEqual(16, stream.Position())
   self.assertEqual(0x0807060504030201, stream.ReadLittleEndian64())
   self.assertEqual(24, stream.Position())
   self.assertEqual(wire_format.UINT64_MAX, stream.ReadLittleEndian64())
   self.assertEqual(32, stream.Position())
   self.assertRaises(message.DecodeError, stream.ReadLittleEndian64)
Exemplo n.º 11
0
 def testEndOfStream(self):
   stream = input_stream.InputStream('abcd')
   self.assertFalse(stream.EndOfStream())
   self.assertEqual('abcd', stream.ReadBytes(10))
   self.assertTrue(stream.EndOfStream())