Skip to content

Commit a44cc0a

Browse files
[3.8] gh-96710: Make the test timing more lenient for the int/str DoS regression test. (GH-96717) (#98197)
gh-96710: Make the test timing more lenient for the int/str DoS regression test. (GH-96717) A regression would still absolutely fail and even a flaky pass isn't harmful as it'd fail most of the time across our N system test runs. Windows has a low resolution timer and CI systems are prone to odd timing so this just gives more leeway to avoid flakiness. (cherry picked from commit 11e3548) Co-authored-by: Gregory P. Smith <[email protected]>
1 parent 4f1364c commit a44cc0a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

Lib/test/test_int.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ def test_denial_of_service_prevented_int_to_str(self):
644644
self.assertEqual(len(huge_decimal), digits)
645645
# Ensuring that we chose a slow enough conversion to measure.
646646
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
647-
if seconds_to_convert < 0.005:
647+
# Some OSes have a low res 1/64s timer, skip if hard to measure.
648+
if seconds_to_convert < 1/64:
648649
raise unittest.SkipTest('"slow" conversion took only '
649650
f'{seconds_to_convert} seconds.')
650651

@@ -656,7 +657,7 @@ def test_denial_of_service_prevented_int_to_str(self):
656657
str(huge_int)
657658
seconds_to_fail_huge = get_time() - start
658659
self.assertIn('conversion', str(err.exception))
659-
self.assertLess(seconds_to_fail_huge, seconds_to_convert/8)
660+
self.assertLessEqual(seconds_to_fail_huge, seconds_to_convert/2)
660661

661662
# Now we test that a conversion that would take 30x as long also fails
662663
# in a similarly fast fashion.
@@ -667,7 +668,7 @@ def test_denial_of_service_prevented_int_to_str(self):
667668
str(extra_huge_int)
668669
seconds_to_fail_extra_huge = get_time() - start
669670
self.assertIn('conversion', str(err.exception))
670-
self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/8)
671+
self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/2)
671672

672673
def test_denial_of_service_prevented_str_to_int(self):
673674
"""Regression test: ensure we fail before performing O(N**2) work."""
@@ -685,7 +686,8 @@ def test_denial_of_service_prevented_str_to_int(self):
685686
seconds_to_convert = get_time() - start
686687
# Ensuring that we chose a slow enough conversion to measure.
687688
# It takes 0.1 seconds on a Zen based cloud VM in an opt build.
688-
if seconds_to_convert < 0.005:
689+
# Some OSes have a low res 1/64s timer, skip if hard to measure.
690+
if seconds_to_convert < 1/64:
689691
raise unittest.SkipTest('"slow" conversion took only '
690692
f'{seconds_to_convert} seconds.')
691693

@@ -695,7 +697,7 @@ def test_denial_of_service_prevented_str_to_int(self):
695697
int(huge)
696698
seconds_to_fail_huge = get_time() - start
697699
self.assertIn('conversion', str(err.exception))
698-
self.assertLess(seconds_to_fail_huge, seconds_to_convert/8)
700+
self.assertLessEqual(seconds_to_fail_huge, seconds_to_convert/2)
699701

700702
# Now we test that a conversion that would take 30x as long also fails
701703
# in a similarly fast fashion.
@@ -706,7 +708,7 @@ def test_denial_of_service_prevented_str_to_int(self):
706708
int(extra_huge)
707709
seconds_to_fail_extra_huge = get_time() - start
708710
self.assertIn('conversion', str(err.exception))
709-
self.assertLess(seconds_to_fail_extra_huge, seconds_to_convert/8)
711+
self.assertLessEqual(seconds_to_fail_extra_huge, seconds_to_convert/2)
710712

711713
def test_power_of_two_bases_unlimited(self):
712714
"""The limit does not apply to power of 2 bases."""

0 commit comments

Comments
 (0)