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
@@ -1431,6 +1432,52 @@ def testMessageClassName(self, message_module):
1431
1432
)
1432
1433
1433
1434
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
+
1434
1481
# Class to test proto2-only features (required, extensions, etc.)
1435
1482
@testing_refleaks.TestCase
1436
1483
class Proto2Test(unittest.TestCase):
@@ -1905,7 +1952,7 @@ def testProto3Optional(self):
1905
1952
if field.name.startswith('optional_'):
1906
1953
self.assertTrue(field.has_presence)
1907
1954
for field in unittest_pb2.TestAllTypes.DESCRIPTOR.fields:
1908
-
if field.is_repeated:
1955
+
if field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
1909
1956
self.assertFalse(field.has_presence)
1910
1957
else:
1911
1958
self.assertTrue(field.has_presence)
@@ -2699,20 +2746,6 @@ def testMapFindInitializationErrorsSmokeTest(self):
2699
2746
msg.map_string_foreign_message['foo'].c = 5
2700
2747
self.assertEqual(0, len(msg.FindInitializationErrors()))
2701
2748
2702
-
def testMapStubReferenceSubMessageDestructor(self):
2703
-
msg = map_unittest_pb2.TestMapSubmessage()
2704
-
# A reference on map stub in sub message
2705
-
map_ref = msg.test_map.map_int32_int32
2706
-
# Make sure destructor after Clear the original message not crash
2707
-
msg.Clear()
2708
-
2709
-
def testRepeatedStubReferenceSubMessageDestructor(self):
2710
-
msg = unittest_pb2.NestedTestAllTypes()
2711
-
# A reference on repeated stub in sub message
2712
-
repeated_ref = msg.payload.repeated_int32
2713
-
# Make sure destructor after Clear the original message not crash
2714
-
msg.Clear()
2715
-
2716
2749
@unittest.skipIf(sys.maxunicode == UCS2_MAXUNICODE, 'Skip for ucs2')
2717
2750
def testStrictUtf8Check(self):
2718
2751
# Test u'\ud801' is rejected at parser in both python2 and python3.
@@ -2873,8 +2906,6 @@ def testUnpackedFields(self):
2873
2906
self.assertEqual(golden_data, message.SerializeToString())
2874
2907
2875
2908
2876
-
@unittest.skipIf(api_implementation.Type() == 'python',
2877
-
'explicit tests of the C++ implementation')
2878
2909
@testing_refleaks.TestCase
2879
2910
class OversizeProtosTest(unittest.TestCase):
2880
2911
@@ -2891,16 +2922,23 @@ def testSucceedOkSizedProto(self):
2891
2922
msg.ParseFromString(self.GenerateNestedProto(100))
2892
2923
2893
2924
def testAssertOversizeProto(self):
2894
-
api_implementation._c_module.SetAllowOversizeProtos(False)
2925
+
if api_implementation.Type() != 'python':
2926
+
api_implementation._c_module.SetAllowOversizeProtos(False)
2895
2927
msg = unittest_pb2.TestRecursiveMessage()
2896
2928
with self.assertRaises(message.DecodeError) as context:
2897
2929
msg.ParseFromString(self.GenerateNestedProto(101))
2898
2930
self.assertIn('Error parsing message', str(context.exception))
2899
2931
2900
2932
def testSucceedOversizeProto(self):
2901
-
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
+
2902
2939
msg = unittest_pb2.TestRecursiveMessage()
2903
2940
msg.ParseFromString(self.GenerateNestedProto(101))
2941
+
decoder.SetRecursionLimit(decoder.DEFAULT_RECURSION_LIMIT)
2904
2942
2905
2943
2906
2944
if __name__ == '__main__':
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4