36
36
from google .protobuf .internal import more_extensions_pb2
37
37
from google .protobuf .internal import more_messages_pb2
38
38
from google .protobuf .internal import packed_field_test_pb2
39
+ from google .protobuf .internal import self_recursive_pb2
39
40
from google .protobuf .internal import test_proto3_optional_pb2
40
41
from google .protobuf .internal import test_util
41
42
from google .protobuf .internal import testing_refleaks
@@ -1339,6 +1340,52 @@ def testIn(self, message_module):
1339
1340
self .assertNotIn ('oneof_string' , m )
1340
1341
1341
1342
1343
+ @testing_refleaks .TestCase
1344
+ class TestRecursiveGroup (unittest .TestCase ):
1345
+
1346
+ def _MakeRecursiveGroupMessage (self , n ):
1347
+ msg = self_recursive_pb2 .SelfRecursive ()
1348
+ sub = msg
1349
+ for _ in range (n ):
1350
+ sub = sub .sub_group
1351
+ sub .i = 1
1352
+ return msg .SerializeToString ()
1353
+
1354
+ def testRecursiveGroups (self ):
1355
+ recurse_msg = self_recursive_pb2 .SelfRecursive ()
1356
+ data = self ._MakeRecursiveGroupMessage (100 )
1357
+ recurse_msg .ParseFromString (data )
1358
+ self .assertTrue (recurse_msg .HasField ('sub_group' ))
1359
+
1360
+ def testRecursiveGroupsException (self ):
1361
+ if api_implementation .Type () != 'python' :
1362
+ api_implementation ._c_module .SetAllowOversizeProtos (False )
1363
+ recurse_msg = self_recursive_pb2 .SelfRecursive ()
1364
+ data = self ._MakeRecursiveGroupMessage (300 )
1365
+ with self .assertRaises (message .DecodeError ) as context :
1366
+ recurse_msg .ParseFromString (data )
1367
+ self .assertIn ('Error parsing message' , str (context .exception ))
1368
+ if api_implementation .Type () == 'python' :
1369
+ self .assertIn ('too many levels of nesting' , str (context .exception ))
1370
+
1371
+ def testRecursiveGroupsUnknownFields (self ):
1372
+ if api_implementation .Type () != 'python' :
1373
+ api_implementation ._c_module .SetAllowOversizeProtos (False )
1374
+ test_msg = unittest_pb2 .TestAllTypes ()
1375
+ data = self ._MakeRecursiveGroupMessage (300 ) # unknown to test_msg
1376
+ with self .assertRaises (message .DecodeError ) as context :
1377
+ test_msg .ParseFromString (data )
1378
+ self .assertIn (
1379
+ 'Error parsing message' ,
1380
+ str (context .exception ),
1381
+ )
1382
+ if api_implementation .Type () == 'python' :
1383
+ self .assertIn ('too many levels of nesting' , str (context .exception ))
1384
+ decoder .SetRecursionLimit (310 )
1385
+ test_msg .ParseFromString (data )
1386
+ decoder .SetRecursionLimit (decoder .DEFAULT_RECURSION_LIMIT )
1387
+
1388
+
1342
1389
# Class to test proto2-only features (required, extensions, etc.)
1343
1390
@testing_refleaks .TestCase
1344
1391
class Proto2Test (unittest .TestCase ):
@@ -2728,8 +2775,6 @@ def testUnpackedFields(self):
2728
2775
self .assertEqual (golden_data , message .SerializeToString ())
2729
2776
2730
2777
2731
- @unittest .skipIf (api_implementation .Type () == 'python' ,
2732
- 'explicit tests of the C++ implementation' )
2733
2778
@testing_refleaks .TestCase
2734
2779
class OversizeProtosTest (unittest .TestCase ):
2735
2780
@@ -2746,16 +2791,23 @@ def testSucceedOkSizedProto(self):
2746
2791
msg .ParseFromString (self .GenerateNestedProto (100 ))
2747
2792
2748
2793
def testAssertOversizeProto (self ):
2749
- api_implementation ._c_module .SetAllowOversizeProtos (False )
2794
+ if api_implementation .Type () != 'python' :
2795
+ api_implementation ._c_module .SetAllowOversizeProtos (False )
2750
2796
msg = unittest_pb2 .TestRecursiveMessage ()
2751
2797
with self .assertRaises (message .DecodeError ) as context :
2752
2798
msg .ParseFromString (self .GenerateNestedProto (101 ))
2753
2799
self .assertIn ('Error parsing message' , str (context .exception ))
2754
2800
2755
2801
def testSucceedOversizeProto (self ):
2756
- api_implementation ._c_module .SetAllowOversizeProtos (True )
2802
+
2803
+ if api_implementation .Type () == 'python' :
2804
+ decoder .SetRecursionLimit (310 )
2805
+ else :
2806
+ api_implementation ._c_module .SetAllowOversizeProtos (True )
2807
+
2757
2808
msg = unittest_pb2 .TestRecursiveMessage ()
2758
2809
msg .ParseFromString (self .GenerateNestedProto (101 ))
2810
+ decoder .SetRecursionLimit (decoder .DEFAULT_RECURSION_LIMIT )
2759
2811
2760
2812
2761
2813
if __name__ == '__main__' :
0 commit comments