minimal_test.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright (c) 2009-2021, Google LLC
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. # * Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # * Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. # * Neither the name of Google LLC nor the
  12. # names of its contributors may be used to endorse or promote products
  13. # derived from this software without specific prior written permission.
  14. #
  15. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. # DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  19. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. """A bare-bones unit test that doesn't load any generated code."""
  26. import unittest
  27. from google.protobuf.pyext import _message
  28. from google.protobuf.internal import api_implementation
  29. from google.protobuf import unittest_pb2
  30. from google.protobuf import map_unittest_pb2
  31. from google.protobuf import descriptor_pool
  32. from google.protobuf import text_format
  33. from google.protobuf import message_factory
  34. from google.protobuf import message
  35. from google.protobuf.internal import factory_test1_pb2
  36. from google.protobuf.internal import factory_test2_pb2
  37. from google.protobuf.internal import more_extensions_pb2
  38. from google.protobuf import descriptor_pb2
  39. class TestMessageExtension(unittest.TestCase):
  40. def test_descriptor_pool(self):
  41. serialized_desc = b'\n\ntest.proto\"\x0e\n\x02M1*\x08\x08\x01\x10\x80\x80\x80\x80\x02:\x15\n\x08test_ext\x12\x03.M1\x18\x01 \x01(\x05'
  42. pool = _message.DescriptorPool()
  43. file_desc = pool.AddSerializedFile(serialized_desc)
  44. self.assertEqual("test.proto", file_desc.name)
  45. ext_desc = pool.FindExtensionByName("test_ext")
  46. self.assertEqual(1, ext_desc.number)
  47. # Test object cache: repeatedly retrieving the same descriptor
  48. # should result in the same object
  49. self.assertIs(ext_desc, pool.FindExtensionByName("test_ext"))
  50. def test_lib_is_upb(self):
  51. # Ensure we are not pulling in a different protobuf library on the
  52. # system.
  53. print(_message._IS_UPB)
  54. self.assertTrue(_message._IS_UPB)
  55. self.assertEqual(api_implementation.Type(), "cpp")
  56. def test_repeated_field_slice_delete(self):
  57. def test_slice(start, end, step):
  58. vals = list(range(20))
  59. message = unittest_pb2.TestAllTypes(repeated_int32=vals)
  60. del vals[start:end:step]
  61. del message.repeated_int32[start:end:step]
  62. self.assertEqual(vals, list(message.repeated_int32))
  63. test_slice(3, 11, 1)
  64. test_slice(3, 11, 2)
  65. test_slice(3, 11, 3)
  66. test_slice(11, 3, -1)
  67. test_slice(11, 3, -2)
  68. test_slice(11, 3, -3)
  69. test_slice(10, 25, 4)
  70. def testExtensionsErrors(self):
  71. msg = unittest_pb2.TestAllTypes()
  72. self.assertRaises(AttributeError, getattr, msg, 'Extensions')
  73. def testClearStubMapField(self):
  74. msg = map_unittest_pb2.TestMapSubmessage()
  75. int32_map = msg.test_map.map_int32_int32
  76. msg.test_map.ClearField("map_int32_int32")
  77. int32_map[123] = 456
  78. self.assertEqual(0, msg.test_map.ByteSize())
  79. def testClearReifiedMapField(self):
  80. msg = map_unittest_pb2.TestMap()
  81. int32_map = msg.map_int32_int32
  82. int32_map[123] = 456
  83. msg.ClearField("map_int32_int32")
  84. int32_map[111] = 222
  85. self.assertEqual(0, msg.ByteSize())
  86. def testClearStubRepeatedField(self):
  87. msg = unittest_pb2.NestedTestAllTypes()
  88. int32_array = msg.payload.repeated_int32
  89. msg.payload.ClearField("repeated_int32")
  90. int32_array.append(123)
  91. self.assertEqual(0, msg.payload.ByteSize())
  92. def testClearReifiedRepeatdField(self):
  93. msg = unittest_pb2.TestAllTypes()
  94. int32_array = msg.repeated_int32
  95. int32_array.append(123)
  96. self.assertNotEqual(0, msg.ByteSize())
  97. msg.ClearField("repeated_int32")
  98. int32_array.append(123)
  99. self.assertEqual(0, msg.ByteSize())
  100. def testFloatPrinting(self):
  101. message = unittest_pb2.TestAllTypes()
  102. message.optional_float = -0.0
  103. self.assertEqual(str(message), 'optional_float: -0\n')
  104. class OversizeProtosTest(unittest.TestCase):
  105. def setUp(self):
  106. msg = unittest_pb2.NestedTestAllTypes()
  107. m = msg
  108. for i in range(101):
  109. m = m.child
  110. m.Clear()
  111. self.p_serialized = msg.SerializeToString()
  112. def testAssertOversizeProto(self):
  113. from google.protobuf.pyext._message import SetAllowOversizeProtos
  114. SetAllowOversizeProtos(False)
  115. q = unittest_pb2.NestedTestAllTypes()
  116. with self.assertRaises(message.DecodeError):
  117. q.ParseFromString(self.p_serialized)
  118. print(q)
  119. def testSucceedOversizeProto(self):
  120. from google.protobuf.pyext._message import SetAllowOversizeProtos
  121. SetAllowOversizeProtos(True)
  122. q = unittest_pb2.NestedTestAllTypes()
  123. q.ParseFromString(self.p_serialized)
  124. def testExtensionIter(self):
  125. extendee_proto = more_extensions_pb2.ExtendedMessage()
  126. extension_int32 = more_extensions_pb2.optional_int_extension
  127. extendee_proto.Extensions[extension_int32] = 23
  128. extension_repeated = more_extensions_pb2.repeated_int_extension
  129. extendee_proto.Extensions[extension_repeated].append(11)
  130. extension_msg = more_extensions_pb2.optional_message_extension
  131. extendee_proto.Extensions[extension_msg].foreign_message_int = 56
  132. # Set some normal fields.
  133. extendee_proto.optional_int32 = 1
  134. extendee_proto.repeated_string.append('hi')
  135. expected = {
  136. extension_int32: True,
  137. extension_msg: True,
  138. extension_repeated: True
  139. }
  140. count = 0
  141. for item in extendee_proto.Extensions:
  142. del expected[item]
  143. self.assertIn(item, extendee_proto.Extensions)
  144. count += 1
  145. self.assertEqual(count, 3)
  146. self.assertEqual(len(expected), 0)
  147. def testIsInitializedStub(self):
  148. proto = unittest_pb2.TestRequiredForeign()
  149. self.assertTrue(proto.IsInitialized())
  150. self.assertFalse(proto.optional_message.IsInitialized())
  151. errors = []
  152. self.assertFalse(proto.optional_message.IsInitialized(errors))
  153. self.assertEqual(['a', 'b', 'c'], errors)
  154. self.assertRaises(message.EncodeError, proto.optional_message.SerializeToString)
  155. if __name__ == '__main__':
  156. unittest.main(verbosity=2)