PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/python/google/protobuf/internal/wire_format_test.py

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