wire_format_test.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #! /usr/bin/env python
  2. #
  3. # Protocol Buffers - Google's data interchange format
  4. # Copyright 2008 Google Inc. All rights reserved.
  5. # https://developers.google.com/protocol-buffers/
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions are
  9. # met:
  10. #
  11. # * Redistributions of source code must retain the above copyright
  12. # notice, this list of conditions and the following disclaimer.
  13. # * Redistributions in binary form must reproduce the above
  14. # copyright notice, this list of conditions and the following disclaimer
  15. # in the documentation and/or other materials provided with the
  16. # distribution.
  17. # * Neither the name of Google Inc. nor the names of its
  18. # contributors may be used to endorse or promote products derived from
  19. # this software without specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. """Test for google.protobuf.internal.wire_format."""
  33. __author__ = 'robinson@google.com (Will Robinson)'
  34. try:
  35. import unittest2 as unittest
  36. except ImportError:
  37. import unittest
  38. from google.protobuf import message
  39. from google.protobuf.internal import wire_format
  40. class WireFormatTest(unittest.TestCase):
  41. def testPackTag(self):
  42. field_number = 0xabc
  43. tag_type = 2
  44. self.assertEqual((field_number << 3) | tag_type,
  45. wire_format.PackTag(field_number, tag_type))
  46. PackTag = wire_format.PackTag
  47. # Number too high.
  48. self.assertRaises(message.EncodeError, PackTag, field_number, 6)
  49. # Number too low.
  50. self.assertRaises(message.EncodeError, PackTag, field_number, -1)
  51. def testUnpackTag(self):
  52. # Test field numbers that will require various varint sizes.
  53. for expected_field_number in (1, 15, 16, 2047, 2048):
  54. for expected_wire_type in range(6): # Highest-numbered wiretype is 5.
  55. field_number, wire_type = wire_format.UnpackTag(
  56. wire_format.PackTag(expected_field_number, expected_wire_type))
  57. self.assertEqual(expected_field_number, field_number)
  58. self.assertEqual(expected_wire_type, wire_type)
  59. self.assertRaises(TypeError, wire_format.UnpackTag, None)
  60. self.assertRaises(TypeError, wire_format.UnpackTag, 'abc')
  61. self.assertRaises(TypeError, wire_format.UnpackTag, 0.0)
  62. self.assertRaises(TypeError, wire_format.UnpackTag, object())
  63. def testZigZagEncode(self):
  64. Z = wire_format.ZigZagEncode
  65. self.assertEqual(0, Z(0))
  66. self.assertEqual(1, Z(-1))
  67. self.assertEqual(2, Z(1))
  68. self.assertEqual(3, Z(-2))
  69. self.assertEqual(4, Z(2))
  70. self.assertEqual(0xfffffffe, Z(0x7fffffff))
  71. self.assertEqual(0xffffffff, Z(-0x80000000))
  72. self.assertEqual(0xfffffffffffffffe, Z(0x7fffffffffffffff))
  73. self.assertEqual(0xffffffffffffffff, Z(-0x8000000000000000))
  74. self.assertRaises(TypeError, Z, None)
  75. self.assertRaises(TypeError, Z, 'abcd')
  76. self.assertRaises(TypeError, Z, 0.0)
  77. self.assertRaises(TypeError, Z, object())
  78. def testZigZagDecode(self):
  79. Z = wire_format.ZigZagDecode
  80. self.assertEqual(0, Z(0))
  81. self.assertEqual(-1, Z(1))
  82. self.assertEqual(1, Z(2))
  83. self.assertEqual(-2, Z(3))
  84. self.assertEqual(2, Z(4))
  85. self.assertEqual(0x7fffffff, Z(0xfffffffe))
  86. self.assertEqual(-0x80000000, Z(0xffffffff))
  87. self.assertEqual(0x7fffffffffffffff, Z(0xfffffffffffffffe))
  88. self.assertEqual(-0x8000000000000000, Z(0xffffffffffffffff))
  89. self.assertRaises(TypeError, Z, None)
  90. self.assertRaises(TypeError, Z, 'abcd')
  91. self.assertRaises(TypeError, Z, 0.0)
  92. self.assertRaises(TypeError, Z, object())
  93. def NumericByteSizeTestHelper(self, byte_size_fn, value, expected_value_size):
  94. # Use field numbers that cause various byte sizes for the tag information.
  95. for field_number, tag_bytes in ((15, 1), (16, 2), (2047, 2), (2048, 3)):
  96. expected_size = expected_value_size + tag_bytes
  97. actual_size = byte_size_fn(field_number, value)
  98. self.assertEqual(expected_size, actual_size,
  99. 'byte_size_fn: %s, field_number: %d, value: %r\n'
  100. 'Expected: %d, Actual: %d'% (
  101. byte_size_fn, field_number, value, expected_size, actual_size))
  102. def testByteSizeFunctions(self):
  103. # Test all numeric *ByteSize() functions.
  104. NUMERIC_ARGS = [
  105. # Int32ByteSize().
  106. [wire_format.Int32ByteSize, 0, 1],
  107. [wire_format.Int32ByteSize, 127, 1],
  108. [wire_format.Int32ByteSize, 128, 2],
  109. [wire_format.Int32ByteSize, -1, 10],
  110. # Int64ByteSize().
  111. [wire_format.Int64ByteSize, 0, 1],
  112. [wire_format.Int64ByteSize, 127, 1],
  113. [wire_format.Int64ByteSize, 128, 2],
  114. [wire_format.Int64ByteSize, -1, 10],
  115. # UInt32ByteSize().
  116. [wire_format.UInt32ByteSize, 0, 1],
  117. [wire_format.UInt32ByteSize, 127, 1],
  118. [wire_format.UInt32ByteSize, 128, 2],
  119. [wire_format.UInt32ByteSize, wire_format.UINT32_MAX, 5],
  120. # UInt64ByteSize().
  121. [wire_format.UInt64ByteSize, 0, 1],
  122. [wire_format.UInt64ByteSize, 127, 1],
  123. [wire_format.UInt64ByteSize, 128, 2],
  124. [wire_format.UInt64ByteSize, wire_format.UINT64_MAX, 10],
  125. # SInt32ByteSize().
  126. [wire_format.SInt32ByteSize, 0, 1],
  127. [wire_format.SInt32ByteSize, -1, 1],
  128. [wire_format.SInt32ByteSize, 1, 1],
  129. [wire_format.SInt32ByteSize, -63, 1],
  130. [wire_format.SInt32ByteSize, 63, 1],
  131. [wire_format.SInt32ByteSize, -64, 1],
  132. [wire_format.SInt32ByteSize, 64, 2],
  133. # SInt64ByteSize().
  134. [wire_format.SInt64ByteSize, 0, 1],
  135. [wire_format.SInt64ByteSize, -1, 1],
  136. [wire_format.SInt64ByteSize, 1, 1],
  137. [wire_format.SInt64ByteSize, -63, 1],
  138. [wire_format.SInt64ByteSize, 63, 1],
  139. [wire_format.SInt64ByteSize, -64, 1],
  140. [wire_format.SInt64ByteSize, 64, 2],
  141. # Fixed32ByteSize().
  142. [wire_format.Fixed32ByteSize, 0, 4],
  143. [wire_format.Fixed32ByteSize, wire_format.UINT32_MAX, 4],
  144. # Fixed64ByteSize().
  145. [wire_format.Fixed64ByteSize, 0, 8],
  146. [wire_format.Fixed64ByteSize, wire_format.UINT64_MAX, 8],
  147. # SFixed32ByteSize().
  148. [wire_format.SFixed32ByteSize, 0, 4],
  149. [wire_format.SFixed32ByteSize, wire_format.INT32_MIN, 4],
  150. [wire_format.SFixed32ByteSize, wire_format.INT32_MAX, 4],
  151. # SFixed64ByteSize().
  152. [wire_format.SFixed64ByteSize, 0, 8],
  153. [wire_format.SFixed64ByteSize, wire_format.INT64_MIN, 8],
  154. [wire_format.SFixed64ByteSize, wire_format.INT64_MAX, 8],
  155. # FloatByteSize().
  156. [wire_format.FloatByteSize, 0.0, 4],
  157. [wire_format.FloatByteSize, 1000000000.0, 4],
  158. [wire_format.FloatByteSize, -1000000000.0, 4],
  159. # DoubleByteSize().
  160. [wire_format.DoubleByteSize, 0.0, 8],
  161. [wire_format.DoubleByteSize, 1000000000.0, 8],
  162. [wire_format.DoubleByteSize, -1000000000.0, 8],
  163. # BoolByteSize().
  164. [wire_format.BoolByteSize, False, 1],
  165. [wire_format.BoolByteSize, True, 1],
  166. # EnumByteSize().
  167. [wire_format.EnumByteSize, 0, 1],
  168. [wire_format.EnumByteSize, 127, 1],
  169. [wire_format.EnumByteSize, 128, 2],
  170. [wire_format.EnumByteSize, wire_format.UINT32_MAX, 5],
  171. ]
  172. for args in NUMERIC_ARGS:
  173. self.NumericByteSizeTestHelper(*args)
  174. # Test strings and bytes.
  175. for byte_size_fn in (wire_format.StringByteSize, wire_format.BytesByteSize):
  176. # 1 byte for tag, 1 byte for length, 3 bytes for contents.
  177. self.assertEqual(5, byte_size_fn(10, 'abc'))
  178. # 2 bytes for tag, 1 byte for length, 3 bytes for contents.
  179. self.assertEqual(6, byte_size_fn(16, 'abc'))
  180. # 2 bytes for tag, 2 bytes for length, 128 bytes for contents.
  181. self.assertEqual(132, byte_size_fn(16, 'a' * 128))
  182. # Test UTF-8 string byte size calculation.
  183. # 1 byte for tag, 1 byte for length, 8 bytes for content.
  184. self.assertEqual(10, wire_format.StringByteSize(
  185. 5, b'\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82'.decode('utf-8')))
  186. class MockMessage(object):
  187. def __init__(self, byte_size):
  188. self.byte_size = byte_size
  189. def ByteSize(self):
  190. return self.byte_size
  191. message_byte_size = 10
  192. mock_message = MockMessage(byte_size=message_byte_size)
  193. # Test groups.
  194. # (2 * 1) bytes for begin and end tags, plus message_byte_size.
  195. self.assertEqual(2 + message_byte_size,
  196. wire_format.GroupByteSize(1, mock_message))
  197. # (2 * 2) bytes for begin and end tags, plus message_byte_size.
  198. self.assertEqual(4 + message_byte_size,
  199. wire_format.GroupByteSize(16, mock_message))
  200. # Test messages.
  201. # 1 byte for tag, plus 1 byte for length, plus contents.
  202. self.assertEqual(2 + mock_message.byte_size,
  203. wire_format.MessageByteSize(1, mock_message))
  204. # 2 bytes for tag, plus 1 byte for length, plus contents.
  205. self.assertEqual(3 + mock_message.byte_size,
  206. wire_format.MessageByteSize(16, mock_message))
  207. # 2 bytes for tag, plus 2 bytes for length, plus contents.
  208. mock_message.byte_size = 128
  209. self.assertEqual(4 + mock_message.byte_size,
  210. wire_format.MessageByteSize(16, mock_message))
  211. # Test message set item byte size.
  212. # 4 bytes for tags, plus 1 byte for length, plus 1 byte for type_id,
  213. # plus contents.
  214. mock_message.byte_size = 10
  215. self.assertEqual(mock_message.byte_size + 6,
  216. wire_format.MessageSetItemByteSize(1, mock_message))
  217. # 4 bytes for tags, plus 2 bytes for length, plus 1 byte for type_id,
  218. # plus contents.
  219. mock_message.byte_size = 128
  220. self.assertEqual(mock_message.byte_size + 7,
  221. wire_format.MessageSetItemByteSize(1, mock_message))
  222. # 4 bytes for tags, plus 2 bytes for length, plus 2 byte for type_id,
  223. # plus contents.
  224. self.assertEqual(mock_message.byte_size + 8,
  225. wire_format.MessageSetItemByteSize(128, mock_message))
  226. # Too-long varint.
  227. self.assertRaises(message.EncodeError,
  228. wire_format.UInt64ByteSize, 1, 1 << 128)
  229. if __name__ == '__main__':
  230. unittest.main()