message_factory_test.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. """Tests for google.protobuf.message_factory."""
  33. __author__ = 'matthewtoia@google.com (Matt Toia)'
  34. try:
  35. import unittest2 as unittest #PY26
  36. except ImportError:
  37. import unittest
  38. from google.protobuf import descriptor_pb2
  39. from google.protobuf.internal import factory_test1_pb2
  40. from google.protobuf.internal import factory_test2_pb2
  41. from google.protobuf import descriptor_database
  42. from google.protobuf import descriptor_pool
  43. from google.protobuf import message_factory
  44. class MessageFactoryTest(unittest.TestCase):
  45. def setUp(self):
  46. self.factory_test1_fd = descriptor_pb2.FileDescriptorProto.FromString(
  47. factory_test1_pb2.DESCRIPTOR.serialized_pb)
  48. self.factory_test2_fd = descriptor_pb2.FileDescriptorProto.FromString(
  49. factory_test2_pb2.DESCRIPTOR.serialized_pb)
  50. def _ExerciseDynamicClass(self, cls):
  51. msg = cls()
  52. msg.mandatory = 42
  53. msg.nested_factory_2_enum = 0
  54. msg.nested_factory_2_message.value = 'nested message value'
  55. msg.factory_1_message.factory_1_enum = 1
  56. msg.factory_1_message.nested_factory_1_enum = 0
  57. msg.factory_1_message.nested_factory_1_message.value = (
  58. 'nested message value')
  59. msg.factory_1_message.scalar_value = 22
  60. msg.factory_1_message.list_value.extend([u'one', u'two', u'three'])
  61. msg.factory_1_message.list_value.append(u'four')
  62. msg.factory_1_enum = 1
  63. msg.nested_factory_1_enum = 0
  64. msg.nested_factory_1_message.value = 'nested message value'
  65. msg.circular_message.mandatory = 1
  66. msg.circular_message.circular_message.mandatory = 2
  67. msg.circular_message.scalar_value = 'one deep'
  68. msg.scalar_value = 'zero deep'
  69. msg.list_value.extend([u'four', u'three', u'two'])
  70. msg.list_value.append(u'one')
  71. msg.grouped.add()
  72. msg.grouped[0].part_1 = 'hello'
  73. msg.grouped[0].part_2 = 'world'
  74. msg.grouped.add(part_1='testing', part_2='123')
  75. msg.loop.loop.mandatory = 2
  76. msg.loop.loop.loop.loop.mandatory = 4
  77. serialized = msg.SerializeToString()
  78. converted = factory_test2_pb2.Factory2Message.FromString(serialized)
  79. reserialized = converted.SerializeToString()
  80. self.assertEqual(serialized, reserialized)
  81. result = cls.FromString(reserialized)
  82. self.assertEqual(msg, result)
  83. def testGetPrototype(self):
  84. db = descriptor_database.DescriptorDatabase()
  85. pool = descriptor_pool.DescriptorPool(db)
  86. db.Add(self.factory_test1_fd)
  87. db.Add(self.factory_test2_fd)
  88. factory = message_factory.MessageFactory()
  89. cls = factory.GetPrototype(pool.FindMessageTypeByName(
  90. 'google.protobuf.python.internal.Factory2Message'))
  91. self.assertFalse(cls is factory_test2_pb2.Factory2Message)
  92. self._ExerciseDynamicClass(cls)
  93. cls2 = factory.GetPrototype(pool.FindMessageTypeByName(
  94. 'google.protobuf.python.internal.Factory2Message'))
  95. self.assertTrue(cls is cls2)
  96. def testGetMessages(self):
  97. # performed twice because multiple calls with the same input must be allowed
  98. for _ in range(2):
  99. messages = message_factory.GetMessages([self.factory_test1_fd,
  100. self.factory_test2_fd])
  101. self.assertTrue(
  102. set(['google.protobuf.python.internal.Factory2Message',
  103. 'google.protobuf.python.internal.Factory1Message'],
  104. ).issubset(set(messages.keys())))
  105. self._ExerciseDynamicClass(
  106. messages['google.protobuf.python.internal.Factory2Message'])
  107. factory_msg1 = messages['google.protobuf.python.internal.Factory1Message']
  108. self.assertTrue(set(
  109. ['google.protobuf.python.internal.Factory2Message.one_more_field',
  110. 'google.protobuf.python.internal.another_field'],).issubset(set(
  111. ext.full_name
  112. for ext in factory_msg1.DESCRIPTOR.file.pool.FindAllExtensions(
  113. factory_msg1.DESCRIPTOR))))
  114. msg1 = messages['google.protobuf.python.internal.Factory1Message']()
  115. ext1 = msg1.Extensions._FindExtensionByName(
  116. 'google.protobuf.python.internal.Factory2Message.one_more_field')
  117. ext2 = msg1.Extensions._FindExtensionByName(
  118. 'google.protobuf.python.internal.another_field')
  119. msg1.Extensions[ext1] = 'test1'
  120. msg1.Extensions[ext2] = 'test2'
  121. self.assertEqual('test1', msg1.Extensions[ext1])
  122. self.assertEqual('test2', msg1.Extensions[ext2])
  123. def testDuplicateExtensionNumber(self):
  124. pool = descriptor_pool.DescriptorPool()
  125. factory = message_factory.MessageFactory(pool=pool)
  126. # Add Container message.
  127. f = descriptor_pb2.FileDescriptorProto()
  128. f.name = 'google/protobuf/internal/container.proto'
  129. f.package = 'google.protobuf.python.internal'
  130. msg = f.message_type.add()
  131. msg.name = 'Container'
  132. rng = msg.extension_range.add()
  133. rng.start = 1
  134. rng.end = 10
  135. pool.Add(f)
  136. msgs = factory.GetMessages([f.name])
  137. self.assertIn('google.protobuf.python.internal.Container', msgs)
  138. # Extend container.
  139. f = descriptor_pb2.FileDescriptorProto()
  140. f.name = 'google/protobuf/internal/extension.proto'
  141. f.package = 'google.protobuf.python.internal'
  142. f.dependency.append('google/protobuf/internal/container.proto')
  143. msg = f.message_type.add()
  144. msg.name = 'Extension'
  145. ext = msg.extension.add()
  146. ext.name = 'extension_field'
  147. ext.number = 2
  148. ext.label = descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL
  149. ext.type_name = 'Extension'
  150. ext.extendee = 'Container'
  151. pool.Add(f)
  152. msgs = factory.GetMessages([f.name])
  153. self.assertIn('google.protobuf.python.internal.Extension', msgs)
  154. # Add Duplicate extending the same field number.
  155. f = descriptor_pb2.FileDescriptorProto()
  156. f.name = 'google/protobuf/internal/duplicate.proto'
  157. f.package = 'google.protobuf.python.internal'
  158. f.dependency.append('google/protobuf/internal/container.proto')
  159. msg = f.message_type.add()
  160. msg.name = 'Duplicate'
  161. ext = msg.extension.add()
  162. ext.name = 'extension_field'
  163. ext.number = 2
  164. ext.label = descriptor_pb2.FieldDescriptorProto.LABEL_OPTIONAL
  165. ext.type_name = 'Duplicate'
  166. ext.extendee = 'Container'
  167. pool.Add(f)
  168. with self.assertRaises(Exception) as cm:
  169. factory.GetMessages([f.name])
  170. self.assertIn(str(cm.exception),
  171. ['Extensions '
  172. '"google.protobuf.python.internal.Duplicate.extension_field" and'
  173. ' "google.protobuf.python.internal.Extension.extension_field"'
  174. ' both try to extend message type'
  175. ' "google.protobuf.python.internal.Container"'
  176. ' with field number 2.',
  177. 'Double registration of Extensions'])
  178. if __name__ == '__main__':
  179. unittest.main()