3636from google .protobuf .internal import more_extensions_pb2
3737from google .protobuf .internal import more_messages_pb2
3838from google .protobuf .internal import packed_field_test_pb2
39+ from google .protobuf .internal import self_recursive_pb2
3940from google .protobuf .internal import test_proto3_optional_pb2
4041from google .protobuf .internal import test_util
4142from google .protobuf .internal import testing_refleaks
@@ -1431,6 +1432,52 @@ def testMessageClassName(self, message_module):
14311432 )
14321433
14331434
1435+ @testing_refleaks .TestCase
1436+ class TestRecursiveGroup (unittest .TestCase ):
1437+
1438+ def _MakeRecursiveGroupMessage (self , n ):
1439+ msg = self_recursive_pb2 .SelfRecursive ()
1440+ sub = msg
1441+ for _ in range (n ):
1442+ sub = sub .sub_group
1443+ sub .i = 1
1444+ return msg .SerializeToString ()
1445+
1446+ def testRecursiveGroups (self ):
1447+ recurse_msg = self_recursive_pb2 .SelfRecursive ()
1448+ data = self ._MakeRecursiveGroupMessage (100 )
1449+ recurse_msg .ParseFromString (data )
1450+ self .assertTrue (recurse_msg .HasField ('sub_group' ))
1451+
1452+ def testRecursiveGroupsException (self ):
1453+ if api_implementation .Type () != 'python' :
1454+ api_implementation ._c_module .SetAllowOversizeProtos (False )
1455+ recurse_msg = self_recursive_pb2 .SelfRecursive ()
1456+ data = self ._MakeRecursiveGroupMessage (300 )
1457+ with self .assertRaises (message .DecodeError ) as context :
1458+ recurse_msg .ParseFromString (data )
1459+ self .assertIn ('Error parsing message' , str (context .exception ))
1460+ if api_implementation .Type () == 'python' :
1461+ self .assertIn ('too many levels of nesting' , str (context .exception ))
1462+
1463+ def testRecursiveGroupsUnknownFields (self ):
1464+ if api_implementation .Type () != 'python' :
1465+ api_implementation ._c_module .SetAllowOversizeProtos (False )
1466+ test_msg = unittest_pb2 .TestAllTypes ()
1467+ data = self ._MakeRecursiveGroupMessage (300 ) # unknown to test_msg
1468+ with self .assertRaises (message .DecodeError ) as context :
1469+ test_msg .ParseFromString (data )
1470+ self .assertIn (
1471+ 'Error parsing message' ,
1472+ str (context .exception ),
1473+ )
1474+ if api_implementation .Type () == 'python' :
1475+ self .assertIn ('too many levels of nesting' , str (context .exception ))
1476+ decoder .SetRecursionLimit (310 )
1477+ test_msg .ParseFromString (data )
1478+ decoder .SetRecursionLimit (decoder .DEFAULT_RECURSION_LIMIT )
1479+
1480+
14341481# Class to test proto2-only features (required, extensions, etc.)
14351482@testing_refleaks .TestCase
14361483class Proto2Test (unittest .TestCase ):
@@ -2859,8 +2906,6 @@ def testUnpackedFields(self):
28592906 self .assertEqual (golden_data , message .SerializeToString ())
28602907
28612908
2862- @unittest .skipIf (api_implementation .Type () == 'python' ,
2863- 'explicit tests of the C++ implementation' )
28642909@testing_refleaks .TestCase
28652910class OversizeProtosTest (unittest .TestCase ):
28662911
@@ -2877,16 +2922,23 @@ def testSucceedOkSizedProto(self):
28772922 msg .ParseFromString (self .GenerateNestedProto (100 ))
28782923
28792924 def testAssertOversizeProto (self ):
2880- api_implementation ._c_module .SetAllowOversizeProtos (False )
2925+ if api_implementation .Type () != 'python' :
2926+ api_implementation ._c_module .SetAllowOversizeProtos (False )
28812927 msg = unittest_pb2 .TestRecursiveMessage ()
28822928 with self .assertRaises (message .DecodeError ) as context :
28832929 msg .ParseFromString (self .GenerateNestedProto (101 ))
28842930 self .assertIn ('Error parsing message' , str (context .exception ))
28852931
28862932 def testSucceedOversizeProto (self ):
2887- api_implementation ._c_module .SetAllowOversizeProtos (True )
2933+
2934+ if api_implementation .Type () == 'python' :
2935+ decoder .SetRecursionLimit (310 )
2936+ else :
2937+ api_implementation ._c_module .SetAllowOversizeProtos (True )
2938+
28882939 msg = unittest_pb2 .TestRecursiveMessage ()
28892940 msg .ParseFromString (self .GenerateNestedProto (101 ))
2941+ decoder .SetRecursionLimit (decoder .DEFAULT_RECURSION_LIMIT )
28902942
28912943
28922944if __name__ == '__main__' :
0 commit comments