def ReadGroupInto_FailureTestHelper(self, bytes_read): d = decoder.Decoder('') d._stream = self.mock_stream sub_buffer = object() self.mock_stream.GetSubBuffer().AndReturn(sub_buffer) self.mock_message.MergeFromString(sub_buffer).AndReturn(bytes_read) return d
def testSkipBytes(self): d = decoder.Decoder('') num_bytes = 1024 self.mock_stream.SkipBytes(num_bytes) d._stream = self.mock_stream self.mox.ReplayAll() d.SkipBytes(num_bytes) self.mox.VerifyAll()
def testReadFieldNumberAndWireType(self): # Test field numbers that will require various varint sizes. for expected_field_number in (1, 15, 16, 2047, 2048): for expected_wire_type in range(6): # Highest-numbered wiretype is 5. e = encoder.Encoder() e.AppendTag(expected_field_number, expected_wire_type) s = e.ToString() d = decoder.Decoder(s) field_number, wire_type = d.ReadFieldNumberAndWireType() self.assertEqual(expected_field_number, field_number) self.assertEqual(expected_wire_type, wire_type)
def testReadGroupInto_Success(self): # Test both the empty and nonempty cases. for num_bytes in (5, 0): field_number = expected_field_number = 10 d = decoder.Decoder('') d._stream = self.mock_stream sub_buffer = object() self.mock_stream.GetSubBuffer().AndReturn(sub_buffer) self.mock_message.MergeFromString(sub_buffer).AndReturn(num_bytes) self.mock_stream.SkipBytes(num_bytes) self.mock_stream.ReadVarUInt32().AndReturn(wire_format.PackTag( field_number, wire_format.WIRETYPE_END_GROUP)) self.mox.ReplayAll() d.ReadGroupInto(expected_field_number, self.mock_message) self.mox.VerifyAll() self.mox.ResetAll()
def ReadScalarTestHelper(self, test_name, decoder_method, expected_result, expected_stream_method_name, stream_method_return, *args): """Helper for testReadScalars below. Calls one of the Decoder.Read*() methods and ensures that the results are as expected. Args: test_name: Name of this test, used for logging only. decoder_method: Unbound decoder.Decoder method to call. expected_result: Value we expect returned from decoder_method(). expected_stream_method_name: (string) Name of the InputStream method we expect Decoder to call to actually read the value on the wire. stream_method_return: Value our mocked-out stream method should return to the decoder. args: Additional arguments that we expect to be passed to the stream method. """ logging.info('Testing %s scalar input.\n' 'Calling %r(), and expecting that to call the ' 'stream method %s(%r), which will return %r. Finally, ' 'expecting the Decoder method to return %r' % (test_name, decoder_method, expected_stream_method_name, args, stream_method_return, expected_result)) d = decoder.Decoder('') d._stream = self.mock_stream if decoder_method in (decoder.Decoder.ReadString, decoder.Decoder.ReadBytes): self.mock_stream.ReadVarUInt32().AndReturn( len(stream_method_return)) # We have to use names instead of methods to work around some # mox weirdness. (ResetAll() is overzealous). expected_stream_method = getattr(self.mock_stream, expected_stream_method_name) expected_stream_method(*args).AndReturn(stream_method_return) self.mox.ReplayAll() result = decoder_method(d) self.assertEqual(expected_result, result) self.assert_(isinstance(result, type(expected_result))) self.mox.VerifyAll() self.mox.ResetAll()
def Test(simulate_error): d = decoder.Decoder('') d._stream = self.mock_stream self.mock_stream.ReadVarUInt32().AndReturn(length) sub_buffer = object() self.mock_stream.GetSubBuffer(length).AndReturn(sub_buffer) if simulate_error: self.mock_message.MergeFromString(sub_buffer).AndReturn(length - 1) self.mox.ReplayAll() self.assertRaises( message.DecodeError, d.ReadMessageInto, self.mock_message) else: self.mock_message.MergeFromString(sub_buffer).AndReturn(length) self.mock_stream.SkipBytes(length) self.mox.ReplayAll() d.ReadMessageInto(self.mock_message) self.mox.VerifyAll() self.mox.ResetAll()