output_stream_test.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Protocol Buffers - Google's data interchange format
  2. # Copyright 2008 Google Inc.
  3. # http://code.google.com/p/protobuf/
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Test for google.protobuf.internal.output_stream."""
  17. __author__ = 'robinson@google.com (Will Robinson)'
  18. import unittest
  19. from google.protobuf import message
  20. from google.protobuf.internal import output_stream
  21. from google.protobuf.internal import wire_format
  22. class OutputStreamTest(unittest.TestCase):
  23. def setUp(self):
  24. self.stream = output_stream.OutputStream()
  25. def testAppendRawBytes(self):
  26. # Empty string.
  27. self.stream.AppendRawBytes('')
  28. self.assertEqual('', self.stream.ToString())
  29. # Nonempty string.
  30. self.stream.AppendRawBytes('abc')
  31. self.assertEqual('abc', self.stream.ToString())
  32. # Ensure that we're actually appending.
  33. self.stream.AppendRawBytes('def')
  34. self.assertEqual('abcdef', self.stream.ToString())
  35. def AppendNumericTestHelper(self, append_fn, values_and_strings):
  36. """For each (value, expected_string) pair in values_and_strings,
  37. calls an OutputStream.Append*(value) method on an OutputStream and ensures
  38. that the string written to that stream matches expected_string.
  39. Args:
  40. append_fn: Unbound OutputStream method that takes an integer or
  41. long value as input.
  42. values_and_strings: Iterable of (value, expected_string) pairs.
  43. """
  44. for conversion in (int, long):
  45. for value, string in values_and_strings:
  46. stream = output_stream.OutputStream()
  47. expected_string = ''
  48. append_fn(stream, conversion(value))
  49. expected_string += string
  50. self.assertEqual(expected_string, stream.ToString())
  51. def AppendOverflowTestHelper(self, append_fn, value):
  52. """Calls an OutputStream.Append*(value) method and asserts
  53. that the method raises message.EncodeError.
  54. Args:
  55. append_fn: Unbound OutputStream method that takes an integer or
  56. long value as input.
  57. value: Value to pass to append_fn which should cause an
  58. message.EncodeError.
  59. """
  60. stream = output_stream.OutputStream()
  61. self.assertRaises(message.EncodeError, append_fn, stream, value)
  62. def testAppendLittleEndian32(self):
  63. append_fn = output_stream.OutputStream.AppendLittleEndian32
  64. values_and_expected_strings = [
  65. (0, '\x00\x00\x00\x00'),
  66. (1, '\x01\x00\x00\x00'),
  67. ((1 << 32) - 1, '\xff\xff\xff\xff'),
  68. ]
  69. self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
  70. self.AppendOverflowTestHelper(append_fn, 1 << 32)
  71. self.AppendOverflowTestHelper(append_fn, -1)
  72. def testAppendLittleEndian64(self):
  73. append_fn = output_stream.OutputStream.AppendLittleEndian64
  74. values_and_expected_strings = [
  75. (0, '\x00\x00\x00\x00\x00\x00\x00\x00'),
  76. (1, '\x01\x00\x00\x00\x00\x00\x00\x00'),
  77. ((1 << 64) - 1, '\xff\xff\xff\xff\xff\xff\xff\xff'),
  78. ]
  79. self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
  80. self.AppendOverflowTestHelper(append_fn, 1 << 64)
  81. self.AppendOverflowTestHelper(append_fn, -1)
  82. def testAppendVarint32(self):
  83. append_fn = output_stream.OutputStream.AppendVarint32
  84. values_and_expected_strings = [
  85. (0, '\x00'),
  86. (1, '\x01'),
  87. (127, '\x7f'),
  88. (128, '\x80\x01'),
  89. (-1, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'),
  90. (wire_format.INT32_MAX, '\xff\xff\xff\xff\x07'),
  91. (wire_format.INT32_MIN, '\x80\x80\x80\x80\xf8\xff\xff\xff\xff\x01'),
  92. ]
  93. self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
  94. self.AppendOverflowTestHelper(append_fn, wire_format.INT32_MAX + 1)
  95. self.AppendOverflowTestHelper(append_fn, wire_format.INT32_MIN - 1)
  96. def testAppendVarUInt32(self):
  97. append_fn = output_stream.OutputStream.AppendVarUInt32
  98. values_and_expected_strings = [
  99. (0, '\x00'),
  100. (1, '\x01'),
  101. (127, '\x7f'),
  102. (128, '\x80\x01'),
  103. (wire_format.UINT32_MAX, '\xff\xff\xff\xff\x0f'),
  104. ]
  105. self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
  106. self.AppendOverflowTestHelper(append_fn, -1)
  107. self.AppendOverflowTestHelper(append_fn, wire_format.UINT32_MAX + 1)
  108. def testAppendVarint64(self):
  109. append_fn = output_stream.OutputStream.AppendVarint64
  110. values_and_expected_strings = [
  111. (0, '\x00'),
  112. (1, '\x01'),
  113. (127, '\x7f'),
  114. (128, '\x80\x01'),
  115. (-1, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'),
  116. (wire_format.INT64_MAX, '\xff\xff\xff\xff\xff\xff\xff\xff\x7f'),
  117. (wire_format.INT64_MIN, '\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01'),
  118. ]
  119. self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
  120. self.AppendOverflowTestHelper(append_fn, wire_format.INT64_MAX + 1)
  121. self.AppendOverflowTestHelper(append_fn, wire_format.INT64_MIN - 1)
  122. def testAppendVarUInt64(self):
  123. append_fn = output_stream.OutputStream.AppendVarUInt64
  124. values_and_expected_strings = [
  125. (0, '\x00'),
  126. (1, '\x01'),
  127. (127, '\x7f'),
  128. (128, '\x80\x01'),
  129. (wire_format.UINT64_MAX, '\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01'),
  130. ]
  131. self.AppendNumericTestHelper(append_fn, values_and_expected_strings)
  132. self.AppendOverflowTestHelper(append_fn, -1)
  133. self.AppendOverflowTestHelper(append_fn, wire_format.UINT64_MAX + 1)
  134. if __name__ == '__main__':
  135. unittest.main()