parent
d5cf7b55a6
commit
fccb146e3f
153 changed files with 11619 additions and 7474 deletions
@ -1,256 +0,0 @@ |
||||
#! /usr/bin/python |
||||
# |
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2008 Google Inc. All rights reserved. |
||||
# http://code.google.com/p/protobuf/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
"""Test for google.protobuf.internal.decoder.""" |
||||
|
||||
__author__ = 'robinson@google.com (Will Robinson)' |
||||
|
||||
import struct |
||||
import unittest |
||||
from google.protobuf.internal import decoder |
||||
from google.protobuf.internal import encoder |
||||
from google.protobuf.internal import input_stream |
||||
from google.protobuf.internal import wire_format |
||||
from google.protobuf import message |
||||
import logging |
||||
import mox |
||||
|
||||
|
||||
class DecoderTest(unittest.TestCase): |
||||
|
||||
def setUp(self): |
||||
self.mox = mox.Mox() |
||||
self.mock_stream = self.mox.CreateMock(input_stream.InputStream) |
||||
self.mock_message = self.mox.CreateMock(message.Message) |
||||
|
||||
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 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() |
||||
|
||||
VAL = 1.125 # Perfectly representable as a float (no rounding error). |
||||
LITTLE_FLOAT_VAL = '\x00\x00\x90?' |
||||
LITTLE_DOUBLE_VAL = '\x00\x00\x00\x00\x00\x00\xf2?' |
||||
|
||||
def testReadScalars(self): |
||||
test_string = 'I can feel myself getting sutpider.' |
||||
scalar_tests = [ |
||||
['int32', decoder.Decoder.ReadInt32, 0, 'ReadVarint32', 0], |
||||
['int64', decoder.Decoder.ReadInt64, 0, 'ReadVarint64', 0], |
||||
['uint32', decoder.Decoder.ReadUInt32, 0, 'ReadVarUInt32', 0], |
||||
['uint64', decoder.Decoder.ReadUInt64, 0, 'ReadVarUInt64', 0], |
||||
['fixed32', decoder.Decoder.ReadFixed32, 0xffffffff, |
||||
'ReadLittleEndian32', 0xffffffff], |
||||
['fixed64', decoder.Decoder.ReadFixed64, 0xffffffffffffffff, |
||||
'ReadLittleEndian64', 0xffffffffffffffff], |
||||
['sfixed32', decoder.Decoder.ReadSFixed32, long(-1), |
||||
'ReadLittleEndian32', long(0xffffffff)], |
||||
['sfixed64', decoder.Decoder.ReadSFixed64, long(-1), |
||||
'ReadLittleEndian64', 0xffffffffffffffff], |
||||
['float', decoder.Decoder.ReadFloat, self.VAL, |
||||
'ReadBytes', self.LITTLE_FLOAT_VAL, 4], |
||||
['double', decoder.Decoder.ReadDouble, self.VAL, |
||||
'ReadBytes', self.LITTLE_DOUBLE_VAL, 8], |
||||
['bool', decoder.Decoder.ReadBool, True, 'ReadVarUInt32', 1], |
||||
['enum', decoder.Decoder.ReadEnum, 23, 'ReadVarUInt32', 23], |
||||
['string', decoder.Decoder.ReadString, |
||||
unicode(test_string, 'utf-8'), 'ReadBytes', test_string, |
||||
len(test_string)], |
||||
['utf8-string', decoder.Decoder.ReadString, |
||||
unicode(test_string, 'utf-8'), 'ReadBytes', test_string, |
||||
len(test_string)], |
||||
['bytes', decoder.Decoder.ReadBytes, |
||||
test_string, 'ReadBytes', test_string, len(test_string)], |
||||
# We test zigzag decoding routines more extensively below. |
||||
['sint32', decoder.Decoder.ReadSInt32, -1, 'ReadVarUInt32', 1], |
||||
['sint64', decoder.Decoder.ReadSInt64, -1, 'ReadVarUInt64', 1], |
||||
] |
||||
# Ensure that we're testing different Decoder methods and using |
||||
# different test names in all test cases above. |
||||
self.assertEqual(len(scalar_tests), len(set(t[0] for t in scalar_tests))) |
||||
self.assert_(len(scalar_tests) >= len(set(t[1] for t in scalar_tests))) |
||||
for args in scalar_tests: |
||||
self.ReadScalarTestHelper(*args) |
||||
|
||||
def testReadMessageInto(self): |
||||
length = 23 |
||||
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() |
||||
|
||||
Test(simulate_error=False) |
||||
Test(simulate_error=True) |
||||
|
||||
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 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 testReadGroupInto_NegativeBytesReported(self): |
||||
expected_field_number = 10 |
||||
d = self.ReadGroupInto_FailureTestHelper(bytes_read=-1) |
||||
self.mox.ReplayAll() |
||||
self.assertRaises(message.DecodeError, |
||||
d.ReadGroupInto, expected_field_number, |
||||
self.mock_message) |
||||
self.mox.VerifyAll() |
||||
|
||||
def testReadGroupInto_NoEndGroupTag(self): |
||||
field_number = expected_field_number = 10 |
||||
num_bytes = 5 |
||||
d = self.ReadGroupInto_FailureTestHelper(bytes_read=num_bytes) |
||||
self.mock_stream.SkipBytes(num_bytes) |
||||
# Right field number, wrong wire type. |
||||
self.mock_stream.ReadVarUInt32().AndReturn(wire_format.PackTag( |
||||
field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)) |
||||
self.mox.ReplayAll() |
||||
self.assertRaises(message.DecodeError, |
||||
d.ReadGroupInto, expected_field_number, |
||||
self.mock_message) |
||||
self.mox.VerifyAll() |
||||
|
||||
def testReadGroupInto_WrongFieldNumberInEndGroupTag(self): |
||||
expected_field_number = 10 |
||||
field_number = expected_field_number + 1 |
||||
num_bytes = 5 |
||||
d = self.ReadGroupInto_FailureTestHelper(bytes_read=num_bytes) |
||||
self.mock_stream.SkipBytes(num_bytes) |
||||
# Wrong field number, right wire type. |
||||
self.mock_stream.ReadVarUInt32().AndReturn(wire_format.PackTag( |
||||
field_number, wire_format.WIRETYPE_END_GROUP)) |
||||
self.mox.ReplayAll() |
||||
self.assertRaises(message.DecodeError, |
||||
d.ReadGroupInto, expected_field_number, |
||||
self.mock_message) |
||||
self.mox.VerifyAll() |
||||
|
||||
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() |
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
@ -1,286 +0,0 @@ |
||||
#! /usr/bin/python |
||||
# |
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2008 Google Inc. All rights reserved. |
||||
# http://code.google.com/p/protobuf/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
"""Test for google.protobuf.internal.encoder.""" |
||||
|
||||
__author__ = 'robinson@google.com (Will Robinson)' |
||||
|
||||
import struct |
||||
import logging |
||||
import unittest |
||||
from google.protobuf.internal import wire_format |
||||
from google.protobuf.internal import encoder |
||||
from google.protobuf.internal import output_stream |
||||
from google.protobuf import message |
||||
import mox |
||||
|
||||
|
||||
class EncoderTest(unittest.TestCase): |
||||
|
||||
def setUp(self): |
||||
self.mox = mox.Mox() |
||||
self.encoder = encoder.Encoder() |
||||
self.mock_stream = self.mox.CreateMock(output_stream.OutputStream) |
||||
self.mock_message = self.mox.CreateMock(message.Message) |
||||
self.encoder._stream = self.mock_stream |
||||
|
||||
def PackTag(self, field_number, wire_type): |
||||
return wire_format.PackTag(field_number, wire_type) |
||||
|
||||
def AppendScalarTestHelper(self, test_name, encoder_method, |
||||
expected_stream_method_name, |
||||
wire_type, field_value, |
||||
expected_value=None, expected_length=None, |
||||
is_tag_test=True): |
||||
"""Helper for testAppendScalars. |
||||
|
||||
Calls one of the Encoder methods, and ensures that the Encoder |
||||
in turn makes the expected calls into its OutputStream. |
||||
|
||||
Args: |
||||
test_name: Name of this test, used only for logging. |
||||
encoder_method: Callable on self.encoder. This is the Encoder |
||||
method we're testing. If is_tag_test=True, the encoder method |
||||
accepts a field_number and field_value. if is_tag_test=False, |
||||
the encoder method accepts a field_value. |
||||
expected_stream_method_name: (string) Name of the OutputStream |
||||
method we expect Encoder to call to actually put the value |
||||
on the wire. |
||||
wire_type: The WIRETYPE_* constant we expect encoder to |
||||
use in the specified encoder_method. |
||||
field_value: The value we're trying to encode. Passed |
||||
into encoder_method. |
||||
expected_value: The value we expect Encoder to pass into |
||||
the OutputStream method. If None, we expect field_value |
||||
to pass through unmodified. |
||||
expected_length: The length we expect Encoder to pass to the |
||||
AppendVarUInt32 method. If None we expect the length of the |
||||
field_value. |
||||
is_tag_test: A Boolean. If True (the default), we append the |
||||
the packed field number and wire_type to the stream before |
||||
the field value. |
||||
""" |
||||
if expected_value is None: |
||||
expected_value = field_value |
||||
|
||||
logging.info('Testing %s scalar output.\n' |
||||
'Calling %r(%r), and expecting that to call the ' |
||||
'stream method %s(%r).' % ( |
||||
test_name, encoder_method, field_value, |
||||
expected_stream_method_name, expected_value)) |
||||
|
||||
if is_tag_test: |
||||
field_number = 10 |
||||
# Should first append the field number and type information. |
||||
self.mock_stream.AppendVarUInt32(self.PackTag(field_number, wire_type)) |
||||
# If we're length-delimited, we should then append the length. |
||||
if wire_type == wire_format.WIRETYPE_LENGTH_DELIMITED: |
||||
if expected_length is None: |
||||
expected_length = len(field_value) |
||||
self.mock_stream.AppendVarUInt32(expected_length) |
||||
|
||||
# Should then append the value itself. |
||||
# 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(expected_value) |
||||
|
||||
self.mox.ReplayAll() |
||||
if is_tag_test: |
||||
encoder_method(field_number, field_value) |
||||
else: |
||||
encoder_method(field_value) |
||||
self.mox.VerifyAll() |
||||
self.mox.ResetAll() |
||||
|
||||
VAL = 1.125 # Perfectly representable as a float (no rounding error). |
||||
LITTLE_FLOAT_VAL = '\x00\x00\x90?' |
||||
LITTLE_DOUBLE_VAL = '\x00\x00\x00\x00\x00\x00\xf2?' |
||||
|
||||
def testAppendScalars(self): |
||||
utf8_bytes = '\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82' |
||||
utf8_string = unicode(utf8_bytes, 'utf-8') |
||||
scalar_tests = [ |
||||
['int32', self.encoder.AppendInt32, 'AppendVarint32', |
||||
wire_format.WIRETYPE_VARINT, 0], |
||||
['int64', self.encoder.AppendInt64, 'AppendVarint64', |
||||
wire_format.WIRETYPE_VARINT, 0], |
||||
['uint32', self.encoder.AppendUInt32, 'AppendVarUInt32', |
||||
wire_format.WIRETYPE_VARINT, 0], |
||||
['uint64', self.encoder.AppendUInt64, 'AppendVarUInt64', |
||||
wire_format.WIRETYPE_VARINT, 0], |
||||
['fixed32', self.encoder.AppendFixed32, 'AppendLittleEndian32', |
||||
wire_format.WIRETYPE_FIXED32, 0], |
||||
['fixed64', self.encoder.AppendFixed64, 'AppendLittleEndian64', |
||||
wire_format.WIRETYPE_FIXED64, 0], |
||||
['sfixed32', self.encoder.AppendSFixed32, 'AppendLittleEndian32', |
||||
wire_format.WIRETYPE_FIXED32, -1, 0xffffffff], |
||||
['sfixed64', self.encoder.AppendSFixed64, 'AppendLittleEndian64', |
||||
wire_format.WIRETYPE_FIXED64, -1, 0xffffffffffffffff], |
||||
['float', self.encoder.AppendFloat, 'AppendRawBytes', |
||||
wire_format.WIRETYPE_FIXED32, self.VAL, self.LITTLE_FLOAT_VAL], |
||||
['double', self.encoder.AppendDouble, 'AppendRawBytes', |
||||
wire_format.WIRETYPE_FIXED64, self.VAL, self.LITTLE_DOUBLE_VAL], |
||||
['bool', self.encoder.AppendBool, 'AppendVarint32', |
||||
wire_format.WIRETYPE_VARINT, False], |
||||
['enum', self.encoder.AppendEnum, 'AppendVarint32', |
||||
wire_format.WIRETYPE_VARINT, 0], |
||||
['string', self.encoder.AppendString, 'AppendRawBytes', |
||||
wire_format.WIRETYPE_LENGTH_DELIMITED, |
||||
"You're in a maze of twisty little passages, all alike."], |
||||
['utf8-string', self.encoder.AppendString, 'AppendRawBytes', |
||||
wire_format.WIRETYPE_LENGTH_DELIMITED, utf8_string, |
||||
utf8_bytes, len(utf8_bytes)], |
||||
# We test zigzag encoding routines more extensively below. |
||||
['sint32', self.encoder.AppendSInt32, 'AppendVarUInt32', |
||||
wire_format.WIRETYPE_VARINT, -1, 1], |
||||
['sint64', self.encoder.AppendSInt64, 'AppendVarUInt64', |
||||
wire_format.WIRETYPE_VARINT, -1, 1], |
||||
] |
||||
# Ensure that we're testing different Encoder methods and using |
||||
# different test names in all test cases above. |
||||
self.assertEqual(len(scalar_tests), len(set(t[0] for t in scalar_tests))) |
||||
self.assert_(len(scalar_tests) >= len(set(t[1] for t in scalar_tests))) |
||||
for args in scalar_tests: |
||||
self.AppendScalarTestHelper(*args) |
||||
|
||||
def testAppendScalarsWithoutTags(self): |
||||
scalar_no_tag_tests = [ |
||||
['int32', self.encoder.AppendInt32NoTag, 'AppendVarint32', None, 0], |
||||
['int64', self.encoder.AppendInt64NoTag, 'AppendVarint64', None, 0], |
||||
['uint32', self.encoder.AppendUInt32NoTag, 'AppendVarUInt32', None, 0], |
||||
['uint64', self.encoder.AppendUInt64NoTag, 'AppendVarUInt64', None, 0], |
||||
['fixed32', self.encoder.AppendFixed32NoTag, |
||||
'AppendLittleEndian32', None, 0], |
||||
['fixed64', self.encoder.AppendFixed64NoTag, |
||||
'AppendLittleEndian64', None, 0], |
||||
['sfixed32', self.encoder.AppendSFixed32NoTag, |
||||
'AppendLittleEndian32', None, 0], |
||||
['sfixed64', self.encoder.AppendSFixed64NoTag, |
||||
'AppendLittleEndian64', None, 0], |
||||
['float', self.encoder.AppendFloatNoTag, |
||||
'AppendRawBytes', None, self.VAL, self.LITTLE_FLOAT_VAL], |
||||
['double', self.encoder.AppendDoubleNoTag, |
||||
'AppendRawBytes', None, self.VAL, self.LITTLE_DOUBLE_VAL], |
||||
['bool', self.encoder.AppendBoolNoTag, 'AppendVarint32', None, 0], |
||||
['enum', self.encoder.AppendEnumNoTag, 'AppendVarint32', None, 0], |
||||
['sint32', self.encoder.AppendSInt32NoTag, |
||||
'AppendVarUInt32', None, -1, 1], |
||||
['sint64', self.encoder.AppendSInt64NoTag, |
||||
'AppendVarUInt64', None, -1, 1], |
||||
] |
||||
|
||||
self.assertEqual(len(scalar_no_tag_tests), |
||||
len(set(t[0] for t in scalar_no_tag_tests))) |
||||
self.assert_(len(scalar_no_tag_tests) >= |
||||
len(set(t[1] for t in scalar_no_tag_tests))) |
||||
for args in scalar_no_tag_tests: |
||||
# For no tag tests, the wire_type is not used, so we put in None. |
||||
self.AppendScalarTestHelper(is_tag_test=False, *args) |
||||
|
||||
def testAppendGroup(self): |
||||
field_number = 23 |
||||
# Should first append the start-group marker. |
||||
self.mock_stream.AppendVarUInt32( |
||||
self.PackTag(field_number, wire_format.WIRETYPE_START_GROUP)) |
||||
# Should then serialize itself. |
||||
self.mock_message.SerializeToString().AndReturn('foo') |
||||
self.mock_stream.AppendRawBytes('foo') |
||||
# Should finally append the end-group marker. |
||||
self.mock_stream.AppendVarUInt32( |
||||
self.PackTag(field_number, wire_format.WIRETYPE_END_GROUP)) |
||||
|
||||
self.mox.ReplayAll() |
||||
self.encoder.AppendGroup(field_number, self.mock_message) |
||||
self.mox.VerifyAll() |
||||
|
||||
def testAppendMessage(self): |
||||
field_number = 23 |
||||
byte_size = 42 |
||||
# Should first append the field number and type information. |
||||
self.mock_stream.AppendVarUInt32( |
||||
self.PackTag(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED)) |
||||
# Should then append its length. |
||||
self.mock_message.ByteSize().AndReturn(byte_size) |
||||
self.mock_stream.AppendVarUInt32(byte_size) |
||||
# Should then serialize itself to the encoder. |
||||
self.mock_message.SerializeToString().AndReturn('foo') |
||||
self.mock_stream.AppendRawBytes('foo') |
||||
|
||||
self.mox.ReplayAll() |
||||
self.encoder.AppendMessage(field_number, self.mock_message) |
||||
self.mox.VerifyAll() |
||||
|
||||
def testAppendMessageSetItem(self): |
||||
field_number = 23 |
||||
byte_size = 42 |
||||
# Should first append the field number and type information. |
||||
self.mock_stream.AppendVarUInt32( |
||||
self.PackTag(1, wire_format.WIRETYPE_START_GROUP)) |
||||
self.mock_stream.AppendVarUInt32( |
||||
self.PackTag(2, wire_format.WIRETYPE_VARINT)) |
||||
self.mock_stream.AppendVarint32(field_number) |
||||
self.mock_stream.AppendVarUInt32( |
||||
self.PackTag(3, wire_format.WIRETYPE_LENGTH_DELIMITED)) |
||||
# Should then append its length. |
||||
self.mock_message.ByteSize().AndReturn(byte_size) |
||||
self.mock_stream.AppendVarUInt32(byte_size) |
||||
# Should then serialize itself to the encoder. |
||||
self.mock_message.SerializeToString().AndReturn('foo') |
||||
self.mock_stream.AppendRawBytes('foo') |
||||
self.mock_stream.AppendVarUInt32( |
||||
self.PackTag(1, wire_format.WIRETYPE_END_GROUP)) |
||||
|
||||
self.mox.ReplayAll() |
||||
self.encoder.AppendMessageSetItem(field_number, self.mock_message) |
||||
self.mox.VerifyAll() |
||||
|
||||
def testAppendSFixed(self): |
||||
# Most of our bounds-checking is done in output_stream.py, |
||||
# but encoder.py is responsible for transforming signed |
||||
# fixed-width integers into unsigned ones, so we test here |
||||
# to ensure that we're not losing any entropy when we do |
||||
# that conversion. |
||||
field_number = 10 |
||||
self.assertRaises(message.EncodeError, self.encoder.AppendSFixed32, |
||||
10, wire_format.UINT32_MAX + 1) |
||||
self.assertRaises(message.EncodeError, self.encoder.AppendSFixed32, |
||||
10, -(1 << 32)) |
||||
self.assertRaises(message.EncodeError, self.encoder.AppendSFixed64, |
||||
10, wire_format.UINT64_MAX + 1) |
||||
self.assertRaises(message.EncodeError, self.encoder.AppendSFixed64, |
||||
10, -(1 << 64)) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
@ -1,338 +0,0 @@ |
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2008 Google Inc. All rights reserved. |
||||
# http://code.google.com/p/protobuf/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
"""InputStream is the primitive interface for reading bits from the wire. |
||||
|
||||
All protocol buffer deserialization can be expressed in terms of |
||||
the InputStream primitives provided here. |
||||
""" |
||||
|
||||
__author__ = 'robinson@google.com (Will Robinson)' |
||||
|
||||
import array |
||||
import struct |
||||
from google.protobuf import message |
||||
from google.protobuf.internal import wire_format |
||||
|
||||
|
||||
# Note that much of this code is ported from //net/proto/ProtocolBuffer, and |
||||
# that the interface is strongly inspired by CodedInputStream from the C++ |
||||
# proto2 implementation. |
||||
|
||||
|
||||
class InputStreamBuffer(object): |
||||
|
||||
"""Contains all logic for reading bits, and dealing with stream position. |
||||
|
||||
If an InputStream method ever raises an exception, the stream is left |
||||
in an indeterminate state and is not safe for further use. |
||||
""" |
||||
|
||||
def __init__(self, s): |
||||
# What we really want is something like array('B', s), where elements we |
||||
# read from the array are already given to us as one-byte integers. BUT |
||||
# using array() instead of buffer() would force full string copies to result |
||||
# from each GetSubBuffer() call. |
||||
# |
||||
# So, if the N serialized bytes of a single protocol buffer object are |
||||
# split evenly between 2 child messages, and so on recursively, using |
||||
# array('B', s) instead of buffer() would incur an additional N*logN bytes |
||||
# copied during deserialization. |
||||
# |
||||
# The higher constant overhead of having to ord() for every byte we read |
||||
# from the buffer in _ReadVarintHelper() could definitely lead to worse |
||||
# performance in many real-world scenarios, even if the asymptotic |
||||
# complexity is better. However, our real answer is that the mythical |
||||
# Python/C extension module output mode for the protocol compiler will |
||||
# be blazing-fast and will eliminate most use of this class anyway. |
||||
self._buffer = buffer(s) |
||||
self._pos = 0 |
||||
|
||||
def EndOfStream(self): |
||||
"""Returns true iff we're at the end of the stream. |
||||
If this returns true, then a call to any other InputStream method |
||||
will raise an exception. |
||||
""" |
||||
return self._pos >= len(self._buffer) |
||||
|
||||
def Position(self): |
||||
"""Returns the current position in the stream, or equivalently, the |
||||
number of bytes read so far. |
||||
""" |
||||
return self._pos |
||||
|
||||
def GetSubBuffer(self, size=None): |
||||
"""Returns a sequence-like object that represents a portion of our |
||||
underlying sequence. |
||||
|
||||
Position 0 in the returned object corresponds to self.Position() |
||||
in this stream. |
||||
|
||||
If size is specified, then the returned object ends after the |
||||
next "size" bytes in this stream. If size is not specified, |
||||
then the returned object ends at the end of this stream. |
||||
|
||||
We guarantee that the returned object R supports the Python buffer |
||||
interface (and thus that the call buffer(R) will work). |
||||
|
||||
Note that the returned buffer is read-only. |
||||
|
||||
The intended use for this method is for nested-message and nested-group |
||||
deserialization, where we want to make a recursive MergeFromString() |
||||
call on the portion of the original sequence that contains the serialized |
||||
nested message. (And we'd like to do so without making unnecessary string |
||||
copies). |
||||
|
||||
REQUIRES: size is nonnegative. |
||||
""" |
||||
# Note that buffer() doesn't perform any actual string copy. |
||||
if size is None: |
||||
return buffer(self._buffer, self._pos) |
||||
else: |
||||
if size < 0: |
||||
raise message.DecodeError('Negative size %d' % size) |
||||
return buffer(self._buffer, self._pos, size) |
||||
|
||||
def SkipBytes(self, num_bytes): |
||||
"""Skip num_bytes bytes ahead, or go to the end of the stream, whichever |
||||
comes first. |
||||
|
||||
REQUIRES: num_bytes is nonnegative. |
||||
""" |
||||
if num_bytes < 0: |
||||
raise message.DecodeError('Negative num_bytes %d' % num_bytes) |
||||
self._pos += num_bytes |
||||
self._pos = min(self._pos, len(self._buffer)) |
||||
|
||||
def ReadBytes(self, size): |
||||
"""Reads up to 'size' bytes from the stream, stopping early |
||||
only if we reach the end of the stream. Returns the bytes read |
||||
as a string. |
||||
""" |
||||
if size < 0: |
||||
raise message.DecodeError('Negative size %d' % size) |
||||
s = (self._buffer[self._pos : self._pos + size]) |
||||
self._pos += len(s) # Only advance by the number of bytes actually read. |
||||
return s |
||||
|
||||
def ReadLittleEndian32(self): |
||||
"""Interprets the next 4 bytes of the stream as a little-endian |
||||
encoded, unsiged 32-bit integer, and returns that integer. |
||||
""" |
||||
try: |
||||
i = struct.unpack(wire_format.FORMAT_UINT32_LITTLE_ENDIAN, |
||||
self._buffer[self._pos : self._pos + 4]) |
||||
self._pos += 4 |
||||
return i[0] # unpack() result is a 1-element tuple. |
||||
except struct.error, e: |
||||
raise message.DecodeError(e) |
||||
|
||||
def ReadLittleEndian64(self): |
||||
"""Interprets the next 8 bytes of the stream as a little-endian |
||||
encoded, unsiged 64-bit integer, and returns that integer. |
||||
""" |
||||
try: |
||||
i = struct.unpack(wire_format.FORMAT_UINT64_LITTLE_ENDIAN, |
||||
self._buffer[self._pos : self._pos + 8]) |
||||
self._pos += 8 |
||||
return i[0] # unpack() result is a 1-element tuple. |
||||
except struct.error, e: |
||||
raise message.DecodeError(e) |
||||
|
||||
def ReadVarint32(self): |
||||
"""Reads a varint from the stream, interprets this varint |
||||
as a signed, 32-bit integer, and returns the integer. |
||||
""" |
||||
i = self.ReadVarint64() |
||||
if not wire_format.INT32_MIN <= i <= wire_format.INT32_MAX: |
||||
raise message.DecodeError('Value out of range for int32: %d' % i) |
||||
return int(i) |
||||
|
||||
def ReadVarUInt32(self): |
||||
"""Reads a varint from the stream, interprets this varint |
||||
as an unsigned, 32-bit integer, and returns the integer. |
||||
""" |
||||
i = self.ReadVarUInt64() |
||||
if i > wire_format.UINT32_MAX: |
||||
raise message.DecodeError('Value out of range for uint32: %d' % i) |
||||
return i |
||||
|
||||
def ReadVarint64(self): |
||||
"""Reads a varint from the stream, interprets this varint |
||||
as a signed, 64-bit integer, and returns the integer. |
||||
""" |
||||
i = self.ReadVarUInt64() |
||||
if i > wire_format.INT64_MAX: |
||||
i -= (1 << 64) |
||||
return i |
||||
|
||||
def ReadVarUInt64(self): |
||||
"""Reads a varint from the stream, interprets this varint |
||||
as an unsigned, 64-bit integer, and returns the integer. |
||||
""" |
||||
i = self._ReadVarintHelper() |
||||
if not 0 <= i <= wire_format.UINT64_MAX: |
||||
raise message.DecodeError('Value out of range for uint64: %d' % i) |
||||
return i |
||||
|
||||
def _ReadVarintHelper(self): |
||||
"""Helper for the various varint-reading methods above. |
||||
Reads an unsigned, varint-encoded integer from the stream and |
||||
returns this integer. |
||||
|
||||
Does no bounds checking except to ensure that we read at most as many bytes |
||||
as could possibly be present in a varint-encoded 64-bit number. |
||||
""" |
||||
result = 0 |
||||
shift = 0 |
||||
while 1: |
||||
if shift >= 64: |
||||
raise message.DecodeError('Too many bytes when decoding varint.') |
||||
try: |
||||
b = ord(self._buffer[self._pos]) |
||||
except IndexError: |
||||
raise message.DecodeError('Truncated varint.') |
||||
self._pos += 1 |
||||
result |= ((b & 0x7f) << shift) |
||||
shift += 7 |
||||
if not (b & 0x80): |
||||
return result |
||||
|
||||
|
||||
class InputStreamArray(object): |
||||
|
||||
"""Contains all logic for reading bits, and dealing with stream position. |
||||
|
||||
If an InputStream method ever raises an exception, the stream is left |
||||
in an indeterminate state and is not safe for further use. |
||||
|
||||
This alternative to InputStreamBuffer is used in environments where buffer() |
||||
is unavailble, such as Google App Engine. |
||||
""" |
||||
|
||||
def __init__(self, s): |
||||
self._buffer = array.array('B', s) |
||||
self._pos = 0 |
||||
|
||||
def EndOfStream(self): |
||||
return self._pos >= len(self._buffer) |
||||
|
||||
def Position(self): |
||||
return self._pos |
||||
|
||||
def GetSubBuffer(self, size=None): |
||||
if size is None: |
||||
return self._buffer[self._pos : ].tostring() |
||||
else: |
||||
if size < 0: |
||||
raise message.DecodeError('Negative size %d' % size) |
||||
return self._buffer[self._pos : self._pos + size].tostring() |
||||
|
||||
def SkipBytes(self, num_bytes): |
||||
if num_bytes < 0: |
||||
raise message.DecodeError('Negative num_bytes %d' % num_bytes) |
||||
self._pos += num_bytes |
||||
self._pos = min(self._pos, len(self._buffer)) |
||||
|
||||
def ReadBytes(self, size): |
||||
if size < 0: |
||||
raise message.DecodeError('Negative size %d' % size) |
||||
s = self._buffer[self._pos : self._pos + size].tostring() |
||||
self._pos += len(s) # Only advance by the number of bytes actually read. |
||||
return s |
||||
|
||||
def ReadLittleEndian32(self): |
||||
try: |
||||
i = struct.unpack(wire_format.FORMAT_UINT32_LITTLE_ENDIAN, |
||||
self._buffer[self._pos : self._pos + 4]) |
||||
self._pos += 4 |
||||
return i[0] # unpack() result is a 1-element tuple. |
||||
except struct.error, e: |
||||
raise message.DecodeError(e) |
||||
|
||||
def ReadLittleEndian64(self): |
||||
try: |
||||
i = struct.unpack(wire_format.FORMAT_UINT64_LITTLE_ENDIAN, |
||||
self._buffer[self._pos : self._pos + 8]) |
||||
self._pos += 8 |
||||
return i[0] # unpack() result is a 1-element tuple. |
||||
except struct.error, e: |
||||
raise message.DecodeError(e) |
||||
|
||||
def ReadVarint32(self): |
||||
i = self.ReadVarint64() |
||||
if not wire_format.INT32_MIN <= i <= wire_format.INT32_MAX: |
||||
raise message.DecodeError('Value out of range for int32: %d' % i) |
||||
return int(i) |
||||
|
||||
def ReadVarUInt32(self): |
||||
i = self.ReadVarUInt64() |
||||
if i > wire_format.UINT32_MAX: |
||||
raise message.DecodeError('Value out of range for uint32: %d' % i) |
||||
return i |
||||
|
||||
def ReadVarint64(self): |
||||
i = self.ReadVarUInt64() |
||||
if i > wire_format.INT64_MAX: |
||||
i -= (1 << 64) |
||||
return i |
||||
|
||||
def ReadVarUInt64(self): |
||||
i = self._ReadVarintHelper() |
||||
if not 0 <= i <= wire_format.UINT64_MAX: |
||||
raise message.DecodeError('Value out of range for uint64: %d' % i) |
||||
return i |
||||
|
||||
def _ReadVarintHelper(self): |
||||
result = 0 |
||||
shift = 0 |
||||
while 1: |
||||
if shift >= 64: |
||||
raise message.DecodeError('Too many bytes when decoding varint.') |
||||
try: |
||||
b = self._buffer[self._pos] |
||||
except IndexError: |
||||
raise message.DecodeError('Truncated varint.') |
||||
self._pos += 1 |
||||
result |= ((b & 0x7f) << shift) |
||||
shift += 7 |
||||
if not (b & 0x80): |
||||
return result |
||||
|
||||
|
||||
try: |
||||
buffer('') |
||||
InputStream = InputStreamBuffer |
||||
except NotImplementedError: |
||||
# Google App Engine: dev_appserver.py |
||||
InputStream = InputStreamArray |
||||
except RuntimeError: |
||||
# Google App Engine: production |
||||
InputStream = InputStreamArray |
@ -1,314 +0,0 @@ |
||||
#! /usr/bin/python |
||||
# |
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2008 Google Inc. All rights reserved. |
||||
# http://code.google.com/p/protobuf/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
"""Test for google.protobuf.internal.input_stream.""" |
||||
|
||||
__author__ = 'robinson@google.com (Will Robinson)' |
||||
|
||||
import unittest |
||||
from google.protobuf import message |
||||
from google.protobuf.internal import wire_format |
||||
from google.protobuf.internal import input_stream |
||||
|
||||
|
||||
class InputStreamBufferTest(unittest.TestCase): |
||||
|
||||
def setUp(self): |
||||
self.__original_input_stream = input_stream.InputStream |
||||
input_stream.InputStream = input_stream.InputStreamBuffer |
||||
|
||||
def tearDown(self): |
||||
input_stream.InputStream = self.__original_input_stream |
||||
|
||||
def testEndOfStream(self): |
||||
stream = input_stream.InputStream('abcd') |
||||
self.assertFalse(stream.EndOfStream()) |
||||
self.assertEqual('abcd', stream.ReadBytes(10)) |
||||
self.assertTrue(stream.EndOfStream()) |
||||
|
||||
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. |
||||
|
||||
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))) |
||||
|
||||
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) |
||||
|
||||
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) |
||||
|
||||
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) |
||||
|
||||
def testReadLittleEndian32(self): |
||||
self.EnsureFailureOnEmptyStream(input_stream.InputStream.ReadLittleEndian32) |
||||
s = '' |
||||
# Read 0. |
||||
s += '\x00\x00\x00\x00' |
||||
# Read 1. |
||||
s += '\x01\x00\x00\x00' |
||||
# Read a bunch of different bytes. |
||||
s += '\x01\x02\x03\x04' |
||||
# Read max unsigned 32-bit int. |
||||
s += '\xff\xff\xff\xff' |
||||
# Try a read with fewer than 4 bytes left in the stream. |
||||
s += '\x00\x00\x00' |
||||
stream = input_stream.InputStream(s) |
||||
self.assertEqual(0, stream.ReadLittleEndian32()) |
||||
self.assertEqual(4, stream.Position()) |
||||
self.assertEqual(1, stream.ReadLittleEndian32()) |
||||
self.assertEqual(8, stream.Position()) |
||||
self.assertEqual(0x04030201, stream.ReadLittleEndian32()) |
||||
self.assertEqual(12, stream.Position()) |
||||
self.assertEqual(wire_format.UINT32_MAX, stream.ReadLittleEndian32()) |
||||
self.assertEqual(16, stream.Position()) |
||||
self.assertRaises(message.DecodeError, stream.ReadLittleEndian32) |
||||
|
||||
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) |
||||
|
||||
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()) |
||||
|
||||
def testReadVarint32Success(self): |
||||
varints_and_ints = [ |
||||
('\x00', 0), |
||||
('\x01', 1), |
||||
('\x7f', 127), |
||||
('\x80\x01', 128), |
||||
('\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01', -1), |
||||
('\xff\xff\xff\xff\x07', wire_format.INT32_MAX), |
||||
('\x80\x80\x80\x80\xf8\xff\xff\xff\xff\x01', wire_format.INT32_MIN), |
||||
] |
||||
self.ReadVarintSuccessTestHelper(varints_and_ints, |
||||
input_stream.InputStream.ReadVarint32) |
||||
|
||||
def testReadVarint32Failure(self): |
||||
self.EnsureFailureOnEmptyStream(input_stream.InputStream.ReadVarint32) |
||||
|
||||
# Try and fail to read INT32_MAX + 1. |
||||
s = '\x80\x80\x80\x80\x08' |
||||
stream = input_stream.InputStream(s) |
||||
self.assertRaises(message.DecodeError, stream.ReadVarint32) |
||||
|
||||
# Try and fail to read INT32_MIN - 1. |
||||
s = '\xfe\xff\xff\xff\xf7\xff\xff\xff\xff\x01' |
||||
stream = input_stream.InputStream(s) |
||||
self.assertRaises(message.DecodeError, stream.ReadVarint32) |
||||
|
||||
# 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.ReadVarint32) |
||||
|
||||
def testReadVarUInt32Success(self): |
||||
varints_and_ints = [ |
||||
('\x00', 0), |
||||
('\x01', 1), |
||||
('\x7f', 127), |
||||
('\x80\x01', 128), |
||||
('\xff\xff\xff\xff\x0f', wire_format.UINT32_MAX), |
||||
] |
||||
self.ReadVarintSuccessTestHelper(varints_and_ints, |
||||
input_stream.InputStream.ReadVarUInt32) |
||||
|
||||
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) |
||||
|
||||
def testReadVarint64Success(self): |
||||
varints_and_ints = [ |
||||
('\x00', 0), |
||||
('\x01', 1), |
||||
('\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01', -1), |
||||
('\x7f', 127), |
||||
('\x80\x01', 128), |
||||
('\xff\xff\xff\xff\xff\xff\xff\xff\x7f', wire_format.INT64_MAX), |
||||
('\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01', wire_format.INT64_MIN), |
||||
] |
||||
self.ReadVarintSuccessTestHelper(varints_and_ints, |
||||
input_stream.InputStream.ReadVarint64) |
||||
|
||||
def testReadVarint64Failure(self): |
||||
self.EnsureFailureOnEmptyStream(input_stream.InputStream.ReadVarint64) |
||||
# 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.ReadVarint64) |
||||
|
||||
# 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.ReadVarint64) |
||||
|
||||
def testReadVarUInt64Success(self): |
||||
varints_and_ints = [ |
||||
('\x00', 0), |
||||
('\x01', 1), |
||||
('\x7f', 127), |
||||
('\x80\x01', 128), |
||||
('\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01', 1 << 63), |
||||
] |
||||
self.ReadVarintSuccessTestHelper(varints_and_ints, |
||||
input_stream.InputStream.ReadVarUInt64) |
||||
|
||||
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) |
||||
|
||||
|
||||
class InputStreamArrayTest(InputStreamBufferTest): |
||||
|
||||
def setUp(self): |
||||
# Test InputStreamArray against the same tests in InputStreamBuffer |
||||
self.__original_input_stream = input_stream.InputStream |
||||
input_stream.InputStream = input_stream.InputStreamArray |
||||
|
||||
def tearDown(self): |
||||
input_stream.InputStream = self.__original_input_stream |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
@ -1,125 +0,0 @@ |
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2008 Google Inc. All rights reserved. |
||||
# http://code.google.com/p/protobuf/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
"""OutputStream is the primitive interface for sticking bits on the wire. |
||||
|
||||
All protocol buffer serialization can be expressed in terms of |
||||
the OutputStream primitives provided here. |
||||
""" |
||||
|
||||
__author__ = 'robinson@google.com (Will Robinson)' |
||||
|
||||
import array |
||||
import struct |
||||
from google.protobuf import message |
||||
from google.protobuf.internal import wire_format |
||||
|
||||
|
||||
|
||||
# Note that much of this code is ported from //net/proto/ProtocolBuffer, and |
||||
# that the interface is strongly inspired by CodedOutputStream from the C++ |
||||
# proto2 implementation. |
||||
|
||||
|
||||
class OutputStream(object): |
||||
|
||||
"""Contains all logic for writing bits, and ToString() to get the result.""" |
||||
|
||||
def __init__(self): |
||||
self._buffer = array.array('B') |
||||
|
||||
def AppendRawBytes(self, raw_bytes): |
||||
"""Appends raw_bytes to our internal buffer.""" |
||||
self._buffer.fromstring(raw_bytes) |
||||
|
||||
def AppendLittleEndian32(self, unsigned_value): |
||||
"""Appends an unsigned 32-bit integer to the internal buffer, |
||||
in little-endian byte order. |
||||
""" |
||||
if not 0 <= unsigned_value <= wire_format.UINT32_MAX: |
||||
raise message.EncodeError( |
||||
'Unsigned 32-bit out of range: %d' % unsigned_value) |
||||
self._buffer.fromstring(struct.pack( |
||||
wire_format.FORMAT_UINT32_LITTLE_ENDIAN, unsigned_value)) |
||||
|
||||
def AppendLittleEndian64(self, unsigned_value): |
||||
"""Appends an unsigned 64-bit integer to the internal buffer, |
||||
in little-endian byte order. |
||||
""" |
||||
if not 0 <= unsigned_value <= wire_format.UINT64_MAX: |
||||
raise message.EncodeError( |
||||
'Unsigned 64-bit out of range: %d' % unsigned_value) |
||||
self._buffer.fromstring(struct.pack( |
||||
wire_format.FORMAT_UINT64_LITTLE_ENDIAN, unsigned_value)) |
||||
|
||||
def AppendVarint32(self, value): |
||||
"""Appends a signed 32-bit integer to the internal buffer, |
||||
encoded as a varint. (Note that a negative varint32 will |
||||
always require 10 bytes of space.) |
||||
""" |
||||
if not wire_format.INT32_MIN <= value <= wire_format.INT32_MAX: |
||||
raise message.EncodeError('Value out of range: %d' % value) |
||||
self.AppendVarint64(value) |
||||
|
||||
def AppendVarUInt32(self, value): |
||||
"""Appends an unsigned 32-bit integer to the internal buffer, |
||||
encoded as a varint. |
||||
""" |
||||
if not 0 <= value <= wire_format.UINT32_MAX: |
||||
raise message.EncodeError('Value out of range: %d' % value) |
||||
self.AppendVarUInt64(value) |
||||
|
||||
def AppendVarint64(self, value): |
||||
"""Appends a signed 64-bit integer to the internal buffer, |
||||
encoded as a varint. |
||||
""" |
||||
if not wire_format.INT64_MIN <= value <= wire_format.INT64_MAX: |
||||
raise message.EncodeError('Value out of range: %d' % value) |
||||
if value < 0: |
||||
value += (1 << 64) |
||||
self.AppendVarUInt64(value) |
||||
|
||||
def AppendVarUInt64(self, unsigned_value): |
||||
"""Appends an unsigned 64-bit integer to the internal buffer, |
||||
encoded as a varint. |
||||
""" |
||||
if not 0 <= unsigned_value <= wire_format.UINT64_MAX: |
||||
raise message.EncodeError('Value out of range: %d' % unsigned_value) |
||||
while True: |
||||
bits = unsigned_value & 0x7f |
||||
unsigned_value >>= 7 |
||||
if not unsigned_value: |
||||
self._buffer.append(bits) |
||||
break |
||||
self._buffer.append(0x80|bits) |
||||
|
||||
def ToString(self): |
||||
"""Returns a string containing the bytes in our internal buffer.""" |
||||
return self._buffer.tostring() |
@ -1,178 +0,0 @@ |
||||
#! /usr/bin/python |
||||
# |
||||
# Protocol Buffers - Google's data interchange format |
||||
# Copyright 2008 Google Inc. All rights reserved. |
||||
# http://code.google.com/p/protobuf/ |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
"""Test for google.protobuf.internal.output_stream.""" |
||||
|
||||
__author__ = 'robinson@google.com (Will Robinson)' |
||||
|
||||
import unittest |
||||
from google.protobuf import message |
||||
from google.protobuf.internal import output_stream |
||||
from google.protobuf.internal import wire_format |
||||
|
||||
|
||||
class OutputStreamTest(unittest.TestCase): |
||||
|
||||
def setUp(self): |
||||
self.stream = output_stream.OutputStream() |
||||
|
||||
def testAppendRawBytes(self): |
||||
# Empty string. |
||||
self.stream.AppendRawBytes('') |
||||
self.assertEqual('', self.stream.ToString()) |
||||
|
||||
# Nonempty string. |
||||
self.stream.AppendRawBytes('abc') |
||||
self.assertEqual('abc', self.stream.ToString()) |
||||
|
||||
# Ensure that we're actually appending. |
||||
self.stream.AppendRawBytes('def') |
||||
self.assertEqual('abcdef', self.stream.ToString()) |
||||
|
||||
def AppendNumericTestHelper(self, append_fn, values_and_strings): |
||||
"""For each (value, expected_string) pair in values_and_strings, |
||||
calls an OutputStream.Append*(value) method on an OutputStream and ensures |
||||
that the string written to that stream matches expected_string. |
||||
|
||||
Args: |
||||
append_fn: Unbound OutputStream method that takes an integer or |
||||
long value as input. |
||||
values_and_strings: Iterable of (value, expected_string) pairs. |
||||
""" |
||||
for conversion in (int, long): |
||||
for value, string in values_and_strings: |
||||
stream = output_stream.OutputStream() |
||||
expected_string = '' |
||||
append_fn(stream, conversion(value)) |
||||
expected_string += string |
||||
self.assertEqual(expected_string, stream.ToString()) |
||||
|
||||
def AppendOverflowTestHelper(self, append_fn, value): |
||||
"""Calls an OutputStream.Append*(value) method and asserts |
||||
that the method raises message.EncodeError. |
||||
|
||||
Args: |
||||
append_fn: Unbound OutputStream method that takes an integer or |
||||
long value as input. |
||||
value: Value to pass to append_fn which should cause an |
||||
message.EncodeError. |
||||
""" |
||||
stream = output_stream.OutputStream() |
||||
self.assertRaises(message.EncodeError, append_fn, stream, value) |
||||
|
||||
def testAppendLittleEndian32(self): |
||||
append_fn = output_stream.OutputStream.AppendLittleEndian32 |
||||
values_and_expected_strings = [ |
||||
(0, '\x00\x00\x00\x00'), |
||||
(1, '\x01\x00\x00\x00'), |
||||
((1 << 32) - 1, '\xff\xff\xff\xff'), |
||||
] |
||||
self.AppendNumericTestHelper(append_fn, values_and_expected_strings) |
||||
|
||||
self.AppendOverflowTestHelper(append_fn, 1 << 32) |
||||
self.AppendOverflowTestHelper(append_fn, -1) |
||||
|
||||
def testAppendLittleEndian64(self): |
||||
append_fn = output_stream.OutputStream.AppendLittleEndian64 |
||||
values_and_expected_strings = [ |
||||
(0, '\x00\x00\x00\x00\x00\x00\x00\x00'), |
||||
(1, '\x01\x00\x00\x00\x00\x00\x00\x00'), |
||||
((1 << 64) - 1, '\xff\xff\xff\xff\xff\xff\xff\xff'), |
||||
] |
||||
self.AppendNumericTestHelper(append_fn, values_and_expected_strings) |
||||
|
||||
self.AppendOverflowTestHelper(append_fn, 1 << 64) |
||||
self.AppendOverflowTestHelper(append_fn, -1) |
||||
|
||||
def testAppendVarint32(self): |
||||
append_fn = output_stream.OutputStream.AppendVarint32 |
||||
values_and_expected_strings = [ |
||||
(0, '\x00'), |
||||
(1, '\x01'), |
||||
(127, '\x7f'), |
||||
(128, '\x80\x01'), |
||||
(-1, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'), |
||||
(wire_format.INT32_MAX, '\xff\xff\xff\xff\x07'), |
||||
(wire_format.INT32_MIN, '\x80\x80\x80\x80\xf8\xff\xff\xff\xff\x01'), |
||||
] |
||||
self.AppendNumericTestHelper(append_fn, values_and_expected_strings) |
||||
|
||||
self.AppendOverflowTestHelper(append_fn, wire_format.INT32_MAX + 1) |
||||
self.AppendOverflowTestHelper(append_fn, wire_format.INT32_MIN - 1) |
||||
|
||||
def testAppendVarUInt32(self): |
||||
append_fn = output_stream.OutputStream.AppendVarUInt32 |
||||
values_and_expected_strings = [ |
||||
(0, '\x00'), |
||||
(1, '\x01'), |
||||
(127, '\x7f'), |
||||
(128, '\x80\x01'), |
||||
(wire_format.UINT32_MAX, '\xff\xff\xff\xff\x0f'), |
||||
] |
||||
self.AppendNumericTestHelper(append_fn, values_and_expected_strings) |
||||
|
||||
self.AppendOverflowTestHelper(append_fn, -1) |
||||
self.AppendOverflowTestHelper(append_fn, wire_format.UINT32_MAX + 1) |
||||
|
||||
def testAppendVarint64(self): |
||||
append_fn = output_stream.OutputStream.AppendVarint64 |
||||
values_and_expected_strings = [ |
||||
(0, '\x00'), |
||||
(1, '\x01'), |
||||
(127, '\x7f'), |
||||
(128, '\x80\x01'), |
||||
(-1, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'), |
||||
(wire_format.INT64_MAX, '\xff\xff\xff\xff\xff\xff\xff\xff\x7f'), |
||||
(wire_format.INT64_MIN, '\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01'), |
||||
] |
||||
self.AppendNumericTestHelper(append_fn, values_and_expected_strings) |
||||
|
||||
self.AppendOverflowTestHelper(append_fn, wire_format.INT64_MAX + 1) |
||||
self.AppendOverflowTestHelper(append_fn, wire_format.INT64_MIN - 1) |
||||
|
||||
def testAppendVarUInt64(self): |
||||
append_fn = output_stream.OutputStream.AppendVarUInt64 |
||||
values_and_expected_strings = [ |
||||
(0, '\x00'), |
||||
(1, '\x01'), |
||||
(127, '\x7f'), |
||||
(128, '\x80\x01'), |
||||
(wire_format.UINT64_MAX, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'), |
||||
] |
||||
self.AppendNumericTestHelper(append_fn, values_and_expected_strings) |
||||
|
||||
self.AppendOverflowTestHelper(append_fn, -1) |
||||
self.AppendOverflowTestHelper(append_fn, wire_format.UINT64_MAX + 1) |
||||
|
||||
|
||||
if __name__ == '__main__': |
||||
unittest.main() |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue