blob: e1690d140cc52046a9a51e396ea8a65210174f8b [file] [log] [blame]
Adam Cozzette5aca7282023-08-07 10:01:08 -07001# Protocol Buffers - Google's data interchange format
2# Copyright 2023 Google LLC. All rights reserved.
3# https://developers.google.com/protocol-buffers/
Joshua Habermanda647532021-08-18 15:30:03 -07004#
5# Redistribution and use in source and binary forms, with or without
Adam Cozzette5aca7282023-08-07 10:01:08 -07006# modification, are permitted provided that the following conditions are
7# met:
Joshua Habermanda647532021-08-18 15:30:03 -07008#
Adam Cozzette5aca7282023-08-07 10:01:08 -07009# * 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 LLC 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.
Joshua Habermanda647532021-08-18 15:30:03 -070030
Joshua Habermancdc9fcd2021-08-23 16:57:40 -070031"""A bare-bones unit test that doesn't load any generated code."""
Joshua Habermana086aaf2021-08-18 15:10:06 -070032
33
34import unittest
35from google.protobuf.pyext import _message
Joshua Haberman2eb973f2022-05-03 16:50:23 -070036from google3.net.proto2.python.internal import api_implementation
Joshua Haberman5f0978a2021-12-28 16:52:43 -080037from google.protobuf import unittest_pb2
Joshua Haberman54b775d2022-01-05 12:55:52 -080038from google.protobuf import map_unittest_pb2
Joshua Habermanb0f81592021-12-29 09:58:12 -080039from google.protobuf import descriptor_pool
Joshua Haberman14372452021-12-31 12:02:36 -080040from google.protobuf import text_format
41from google.protobuf import message_factory
42from google.protobuf import message
Joshua Haberman2eb973f2022-05-03 16:50:23 -070043from google3.net.proto2.python.internal import factory_test1_pb2
44from google3.net.proto2.python.internal import factory_test2_pb2
45from google3.net.proto2.python.internal import more_extensions_pb2
Joshua Habermanb0f81592021-12-29 09:58:12 -080046from google.protobuf import descriptor_pb2
Joshua Habermana086aaf2021-08-18 15:10:06 -070047
48class TestMessageExtension(unittest.TestCase):
49
50 def test_descriptor_pool(self):
51 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'
52 pool = _message.DescriptorPool()
53 file_desc = pool.AddSerializedFile(serialized_desc)
54 self.assertEqual("test.proto", file_desc.name)
55 ext_desc = pool.FindExtensionByName("test_ext")
56 self.assertEqual(1, ext_desc.number)
57
58 # Test object cache: repeatedly retrieving the same descriptor
59 # should result in the same object
60 self.assertIs(ext_desc, pool.FindExtensionByName("test_ext"))
61
62
63 def test_lib_is_upb(self):
64 # Ensure we are not pulling in a different protobuf library on the
65 # system.
Joshua Habermane079f1b2021-12-18 22:10:46 -080066 print(_message._IS_UPB)
Joshua Habermana086aaf2021-08-18 15:10:06 -070067 self.assertTrue(_message._IS_UPB)
Joshua Habermane079f1b2021-12-18 22:10:46 -080068 self.assertEqual(api_implementation.Type(), "cpp")
Joshua Habermana086aaf2021-08-18 15:10:06 -070069
Joshua Haberman5f0978a2021-12-28 16:52:43 -080070 def test_repeated_field_slice_delete(self):
71 def test_slice(start, end, step):
72 vals = list(range(20))
73 message = unittest_pb2.TestAllTypes(repeated_int32=vals)
74 del vals[start:end:step]
75 del message.repeated_int32[start:end:step]
76 self.assertEqual(vals, list(message.repeated_int32))
77 test_slice(3, 11, 1)
78 test_slice(3, 11, 2)
79 test_slice(3, 11, 3)
80 test_slice(11, 3, -1)
81 test_slice(11, 3, -2)
82 test_slice(11, 3, -3)
83 test_slice(10, 25, 4)
Joshua Haberman14372452021-12-31 12:02:36 -080084
85 def testExtensionsErrors(self):
86 msg = unittest_pb2.TestAllTypes()
87 self.assertRaises(AttributeError, getattr, msg, 'Extensions')
Joshua Haberman54b775d2022-01-05 12:55:52 -080088
89 def testClearStubMapField(self):
Joshua Habermane5d8d282022-01-05 15:47:59 -080090 msg = map_unittest_pb2.TestMapSubmessage()
91 int32_map = msg.test_map.map_int32_int32
92 msg.test_map.ClearField("map_int32_int32")
Joshua Haberman54b775d2022-01-05 12:55:52 -080093 int32_map[123] = 456
Joshua Habermane5d8d282022-01-05 15:47:59 -080094 self.assertEqual(0, msg.test_map.ByteSize())
Joshua Haberman54b775d2022-01-05 12:55:52 -080095
96 def testClearReifiedMapField(self):
97 msg = map_unittest_pb2.TestMap()
98 int32_map = msg.map_int32_int32
99 int32_map[123] = 456
100 msg.ClearField("map_int32_int32")
101 int32_map[111] = 222
102 self.assertEqual(0, msg.ByteSize())
103
104 def testClearStubRepeatedField(self):
Joshua Habermane5d8d282022-01-05 15:47:59 -0800105 msg = unittest_pb2.NestedTestAllTypes()
106 int32_array = msg.payload.repeated_int32
107 msg.payload.ClearField("repeated_int32")
Joshua Haberman54b775d2022-01-05 12:55:52 -0800108 int32_array.append(123)
Joshua Habermane5d8d282022-01-05 15:47:59 -0800109 self.assertEqual(0, msg.payload.ByteSize())
Joshua Haberman54b775d2022-01-05 12:55:52 -0800110
111 def testClearReifiedRepeatdField(self):
112 msg = unittest_pb2.TestAllTypes()
113 int32_array = msg.repeated_int32
114 int32_array.append(123)
115 self.assertNotEqual(0, msg.ByteSize())
116 msg.ClearField("repeated_int32")
117 int32_array.append(123)
118 self.assertEqual(0, msg.ByteSize())
Joshua Haberman5f0978a2021-12-28 16:52:43 -0800119
Joshua Haberman06d6f5c2022-01-17 15:38:25 -0800120 def testFloatPrinting(self):
121 message = unittest_pb2.TestAllTypes()
122 message.optional_float = -0.0
123 self.assertEqual(str(message), 'optional_float: -0\n')
Joshua Habermanb8736ae2021-12-13 12:51:10 -0800124
Joshua Haberman14372452021-12-31 12:02:36 -0800125class OversizeProtosTest(unittest.TestCase):
126 def setUp(self):
127 msg = unittest_pb2.NestedTestAllTypes()
128 m = msg
129 for i in range(101):
130 m = m.child
131 m.Clear()
132 self.p_serialized = msg.SerializeToString()
133
134 def testAssertOversizeProto(self):
135 from google.protobuf.pyext._message import SetAllowOversizeProtos
136 SetAllowOversizeProtos(False)
137 q = unittest_pb2.NestedTestAllTypes()
138 with self.assertRaises(message.DecodeError):
139 q.ParseFromString(self.p_serialized)
140 print(q)
141
142 def testSucceedOversizeProto(self):
143 from google.protobuf.pyext._message import SetAllowOversizeProtos
144 SetAllowOversizeProtos(True)
145 q = unittest_pb2.NestedTestAllTypes()
146 q.ParseFromString(self.p_serialized)
Joshua Habermana086aaf2021-08-18 15:10:06 -0700147
Joshua Haberman71bfff22022-01-09 11:22:53 -0800148 def testExtensionIter(self):
149 extendee_proto = more_extensions_pb2.ExtendedMessage()
150
151 extension_int32 = more_extensions_pb2.optional_int_extension
152 extendee_proto.Extensions[extension_int32] = 23
Joshua Habermancfeb1ad2022-01-09 22:44:31 -0800153
Joshua Haberman71bfff22022-01-09 11:22:53 -0800154 extension_repeated = more_extensions_pb2.repeated_int_extension
155 extendee_proto.Extensions[extension_repeated].append(11)
156
157 extension_msg = more_extensions_pb2.optional_message_extension
158 extendee_proto.Extensions[extension_msg].foreign_message_int = 56
Joshua Habermancfeb1ad2022-01-09 22:44:31 -0800159
Joshua Haberman71bfff22022-01-09 11:22:53 -0800160 # Set some normal fields.
161 extendee_proto.optional_int32 = 1
162 extendee_proto.repeated_string.append('hi')
Joshua Habermancfeb1ad2022-01-09 22:44:31 -0800163
Joshua Haberman71bfff22022-01-09 11:22:53 -0800164 expected = {
165 extension_int32: True,
166 extension_msg: True,
167 extension_repeated: True
168 }
169 count = 0
170 for item in extendee_proto.Extensions:
171 del expected[item]
172 self.assertIn(item, extendee_proto.Extensions)
173 count += 1
174 self.assertEqual(count, 3)
175 self.assertEqual(len(expected), 0)
Joshua Haberman8c256cc2022-01-10 10:00:10 -0800176
177 def testIsInitializedStub(self):
178 proto = unittest_pb2.TestRequiredForeign()
179 self.assertTrue(proto.IsInitialized())
180 self.assertFalse(proto.optional_message.IsInitialized())
181 errors = []
182 self.assertFalse(proto.optional_message.IsInitialized(errors))
183 self.assertEqual(['a', 'b', 'c'], errors)
184 self.assertRaises(message.EncodeError, proto.optional_message.SerializeToString)
Joshua Haberman917f4252022-01-09 23:23:36 -0800185
Joshua Habermana086aaf2021-08-18 15:10:06 -0700186if __name__ == '__main__':
Joshua Habermanb8736ae2021-12-13 12:51:10 -0800187 unittest.main(verbosity=2)