/thirdparty/breakpad/third_party/protobuf/protobuf/python/google/protobuf/internal/wire_format_test.py

http://github.com/tomahawk-player/tomahawk · Python · 253 lines · 155 code · 26 blank · 72 comment · 6 complexity · 9631d9090165366dafed6011cebc9ff8 MD5 · raw file

  1. #! /usr/bin/python
  2. #
  3. # Protocol Buffers - Google's data interchange format
  4. # Copyright 2008 Google Inc. All rights reserved.
  5. # http://code.google.com/p/protobuf/
  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. import unittest
  35. from google.protobuf import message
  36. from google.protobuf.internal import wire_format
  37. class WireFormatTest(unittest.TestCase):
  38. def testPackTag(self):
  39. field_number = 0xabc
  40. tag_type = 2
  41. self.assertEqual((field_number << 3) | tag_type,
  42. wire_format.PackTag(field_number, tag_type))
  43. PackTag = wire_format.PackTag
  44. # Number too high.
  45. self.assertRaises(message.EncodeError, PackTag, field_number, 6)
  46. # Number too low.
  47. self.assertRaises(message.EncodeError, PackTag, field_number, -1)
  48. def testUnpackTag(self):
  49. # Test field numbers that will require various varint sizes.
  50. for expected_field_number in (1, 15, 16, 2047, 2048):
  51. for expected_wire_type in range(6): # Highest-numbered wiretype is 5.
  52. field_number, wire_type = wire_format.UnpackTag(
  53. wire_format.PackTag(expected_field_number, expected_wire_type))
  54. self.assertEqual(expected_field_number, field_number)
  55. self.assertEqual(expected_wire_type, wire_type)
  56. self.assertRaises(TypeError, wire_format.UnpackTag, None)
  57. self.assertRaises(TypeError, wire_format.UnpackTag, 'abc')
  58. self.assertRaises(TypeError, wire_format.UnpackTag, 0.0)
  59. self.assertRaises(TypeError, wire_format.UnpackTag, object())
  60. def testZigZagEncode(self):
  61. Z = wire_format.ZigZagEncode
  62. self.assertEqual(0, Z(0))
  63. self.assertEqual(1, Z(-1))
  64. self.assertEqual(2, Z(1))
  65. self.assertEqual(3, Z(-2))
  66. self.assertEqual(4, Z(2))
  67. self.assertEqual(0xfffffffe, Z(0x7fffffff))
  68. self.assertEqual(0xffffffff, Z(-0x80000000))
  69. self.assertEqual(0xfffffffffffffffe, Z(0x7fffffffffffffff))
  70. self.assertEqual(0xffffffffffffffff, Z(-0x8000000000000000))
  71. self.assertRaises(TypeError, Z, None)
  72. self.assertRaises(TypeError, Z, 'abcd')
  73. self.assertRaises(TypeError, Z, 0.0)
  74. self.assertRaises(TypeError, Z, object())
  75. def testZigZagDecode(self):
  76. Z = wire_format.ZigZagDecode
  77. self.assertEqual(0, Z(0))
  78. self.assertEqual(-1, Z(1))
  79. self.assertEqual(1, Z(2))
  80. self.assertEqual(-2, Z(3))
  81. self.assertEqual(2, Z(4))
  82. self.assertEqual(0x7fffffff, Z(0xfffffffe))
  83. self.assertEqual(-0x80000000, Z(0xffffffff))
  84. self.assertEqual(0x7fffffffffffffff, Z(0xfffffffffffffffe))
  85. self.assertEqual(-0x8000000000000000, Z(0xffffffffffffffff))
  86. self.assertRaises(TypeError, Z, None)
  87. self.assertRaises(TypeError, Z, 'abcd')
  88. self.assertRaises(TypeError, Z, 0.0)
  89. self.assertRaises(TypeError, Z, object())
  90. def NumericByteSizeTestHelper(self, byte_size_fn, value, expected_value_size):
  91. # Use field numbers that cause various byte sizes for the tag information.
  92. for field_number, tag_bytes in ((15, 1), (16, 2), (2047, 2), (2048, 3)):
  93. expected_size = expected_value_size + tag_bytes
  94. actual_size = byte_size_fn(field_number, value)
  95. self.assertEqual(expected_size, actual_size,
  96. 'byte_size_fn: %s, field_number: %d, value: %r\n'
  97. 'Expected: %d, Actual: %d'% (
  98. byte_size_fn, field_number, value, expected_size, actual_size))
  99. def testByteSizeFunctions(self):
  100. # Test all numeric *ByteSize() functions.
  101. NUMERIC_ARGS = [
  102. # Int32ByteSize().
  103. [wire_format.Int32ByteSize, 0, 1],
  104. [wire_format.Int32ByteSize, 127, 1],
  105. [wire_format.Int32ByteSize, 128, 2],
  106. [wire_format.Int32ByteSize, -1, 10],
  107. # Int64ByteSize().
  108. [wire_format.Int64ByteSize, 0, 1],
  109. [wire_format.Int64ByteSize, 127, 1],
  110. [wire_format.Int64ByteSize, 128, 2],
  111. [wire_format.Int64ByteSize, -1, 10],
  112. # UInt32ByteSize().
  113. [wire_format.UInt32ByteSize, 0, 1],
  114. [wire_format.UInt32ByteSize, 127, 1],
  115. [wire_format.UInt32ByteSize, 128, 2],
  116. [wire_format.UInt32ByteSize, wire_format.UINT32_MAX, 5],
  117. # UInt64ByteSize().
  118. [wire_format.UInt64ByteSize, 0, 1],
  119. [wire_format.UInt64ByteSize, 127, 1],
  120. [wire_format.UInt64ByteSize, 128, 2],
  121. [wire_format.UInt64ByteSize, wire_format.UINT64_MAX, 10],
  122. # SInt32ByteSize().
  123. [wire_format.SInt32ByteSize, 0, 1],
  124. [wire_format.SInt32ByteSize, -1, 1],
  125. [wire_format.SInt32ByteSize, 1, 1],
  126. [wire_format.SInt32ByteSize, -63, 1],
  127. [wire_format.SInt32ByteSize, 63, 1],
  128. [wire_format.SInt32ByteSize, -64, 1],
  129. [wire_format.SInt32ByteSize, 64, 2],
  130. # SInt64ByteSize().
  131. [wire_format.SInt64ByteSize, 0, 1],
  132. [wire_format.SInt64ByteSize, -1, 1],
  133. [wire_format.SInt64ByteSize, 1, 1],
  134. [wire_format.SInt64ByteSize, -63, 1],
  135. [wire_format.SInt64ByteSize, 63, 1],
  136. [wire_format.SInt64ByteSize, -64, 1],
  137. [wire_format.SInt64ByteSize, 64, 2],
  138. # Fixed32ByteSize().
  139. [wire_format.Fixed32ByteSize, 0, 4],
  140. [wire_format.Fixed32ByteSize, wire_format.UINT32_MAX, 4],
  141. # Fixed64ByteSize().
  142. [wire_format.Fixed64ByteSize, 0, 8],
  143. [wire_format.Fixed64ByteSize, wire_format.UINT64_MAX, 8],
  144. # SFixed32ByteSize().
  145. [wire_format.SFixed32ByteSize, 0, 4],
  146. [wire_format.SFixed32ByteSize, wire_format.INT32_MIN, 4],
  147. [wire_format.SFixed32ByteSize, wire_format.INT32_MAX, 4],
  148. # SFixed64ByteSize().
  149. [wire_format.SFixed64ByteSize, 0, 8],
  150. [wire_format.SFixed64ByteSize, wire_format.INT64_MIN, 8],
  151. [wire_format.SFixed64ByteSize, wire_format.INT64_MAX, 8],
  152. # FloatByteSize().
  153. [wire_format.FloatByteSize, 0.0, 4],
  154. [wire_format.FloatByteSize, 1000000000.0, 4],
  155. [wire_format.FloatByteSize, -1000000000.0, 4],
  156. # DoubleByteSize().
  157. [wire_format.DoubleByteSize, 0.0, 8],
  158. [wire_format.DoubleByteSize, 1000000000.0, 8],
  159. [wire_format.DoubleByteSize, -1000000000.0, 8],
  160. # BoolByteSize().
  161. [wire_format.BoolByteSize, False, 1],
  162. [wire_format.BoolByteSize, True, 1],
  163. # EnumByteSize().
  164. [wire_format.EnumByteSize, 0, 1],
  165. [wire_format.EnumByteSize, 127, 1],
  166. [wire_format.EnumByteSize, 128, 2],
  167. [wire_format.EnumByteSize, wire_format.UINT32_MAX, 5],
  168. ]
  169. for args in NUMERIC_ARGS:
  170. self.NumericByteSizeTestHelper(*args)
  171. # Test strings and bytes.
  172. for byte_size_fn in (wire_format.StringByteSize, wire_format.BytesByteSize):
  173. # 1 byte for tag, 1 byte for length, 3 bytes for contents.
  174. self.assertEqual(5, byte_size_fn(10, 'abc'))
  175. # 2 bytes for tag, 1 byte for length, 3 bytes for contents.
  176. self.assertEqual(6, byte_size_fn(16, 'abc'))
  177. # 2 bytes for tag, 2 bytes for length, 128 bytes for contents.
  178. self.assertEqual(132, byte_size_fn(16, 'a' * 128))
  179. # Test UTF-8 string byte size calculation.
  180. # 1 byte for tag, 1 byte for length, 8 bytes for content.
  181. self.assertEqual(10, wire_format.StringByteSize(
  182. 5, unicode('\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82', 'utf-8')))
  183. class MockMessage(object):
  184. def __init__(self, byte_size):
  185. self.byte_size = byte_size
  186. def ByteSize(self):
  187. return self.byte_size
  188. message_byte_size = 10
  189. mock_message = MockMessage(byte_size=message_byte_size)
  190. # Test groups.
  191. # (2 * 1) bytes for begin and end tags, plus message_byte_size.
  192. self.assertEqual(2 + message_byte_size,
  193. wire_format.GroupByteSize(1, mock_message))
  194. # (2 * 2) bytes for begin and end tags, plus message_byte_size.
  195. self.assertEqual(4 + message_byte_size,
  196. wire_format.GroupByteSize(16, mock_message))
  197. # Test messages.
  198. # 1 byte for tag, plus 1 byte for length, plus contents.
  199. self.assertEqual(2 + mock_message.byte_size,
  200. wire_format.MessageByteSize(1, mock_message))
  201. # 2 bytes for tag, plus 1 byte for length, plus contents.
  202. self.assertEqual(3 + mock_message.byte_size,
  203. wire_format.MessageByteSize(16, mock_message))
  204. # 2 bytes for tag, plus 2 bytes for length, plus contents.
  205. mock_message.byte_size = 128
  206. self.assertEqual(4 + mock_message.byte_size,
  207. wire_format.MessageByteSize(16, mock_message))
  208. # Test message set item byte size.
  209. # 4 bytes for tags, plus 1 byte for length, plus 1 byte for type_id,
  210. # plus contents.
  211. mock_message.byte_size = 10
  212. self.assertEqual(mock_message.byte_size + 6,
  213. wire_format.MessageSetItemByteSize(1, mock_message))
  214. # 4 bytes for tags, plus 2 bytes for length, plus 1 byte for type_id,
  215. # plus contents.
  216. mock_message.byte_size = 128
  217. self.assertEqual(mock_message.byte_size + 7,
  218. wire_format.MessageSetItemByteSize(1, mock_message))
  219. # 4 bytes for tags, plus 2 bytes for length, plus 2 byte for type_id,
  220. # plus contents.
  221. self.assertEqual(mock_message.byte_size + 8,
  222. wire_format.MessageSetItemByteSize(128, mock_message))
  223. # Too-long varint.
  224. self.assertRaises(message.EncodeError,
  225. wire_format.UInt64ByteSize, 1, 1 << 128)
  226. if __name__ == '__main__':
  227. unittest.main()