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