Skip to content

Commit e454e42

Browse files
committed
move the _hashlib get_fips_mode logic check into test.support.
1 parent bd4a172 commit e454e42

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

Lib/test/support/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2969,3 +2969,11 @@ def run_yielding_async_fn(async_fn, /, *args, **kwargs):
29692969
return e.value
29702970
finally:
29712971
coro.close()
2972+
2973+
2974+
def is_libssl_fips_mode():
2975+
try:
2976+
from _hashlib import get_fips_mode # ask _hashopenssl.c
2977+
except ImportError:
2978+
return False # more of a maybe, unless we add this to the _ssl module.
2979+
return get_fips_mode() != 0

Lib/test/test_urllib2.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
import urllib.error
2828
import http.client
2929

30-
try:
31-
from _hashlib import get_fips_mode
32-
except ImportError:
33-
def get_fips_mode():
34-
return 0
3530

3631
support.requires_working_socket(module=True)
3732

@@ -1969,23 +1964,29 @@ def test_parse_proxy(self):
19691964
self.assertRaises(ValueError, _parse_proxy, 'file:/ftp.example.com'),
19701965

19711966

1967+
skip_libssl_fips_mode = unittest.skipIf(
1968+
support.is_libssl_fips_mode(),
1969+
"conservative skip due to OpenSSL FIPS mode possible algorithm nerfing",
1970+
)
1971+
1972+
19721973
class TestDigestAuthAlgorithms(unittest.TestCase):
19731974
def setUp(self):
19741975
self.handler = AbstractDigestAuthHandler()
19751976

1976-
@unittest.skipIf(get_fips_mode(), "fips mode; requires hashlib.md5")
1977+
@skip_libssl_fips_mode
19771978
def test_md5_algorithm(self):
19781979
H, KD = self.handler.get_algorithm_impls('MD5')
19791980
self.assertEqual(H("foo"), "acbd18db4cc2f85cedef654fccc4a4d8")
19801981
self.assertEqual(KD("foo", "bar"), "4e99e8c12de7e01535248d2bac85e732")
19811982

1982-
@unittest.skipIf(get_fips_mode(), "fips mode; requires hashlib.sha1")
1983+
@skip_libssl_fips_mode
19831984
def test_sha_algorithm(self):
19841985
H, KD = self.handler.get_algorithm_impls('SHA')
19851986
self.assertEqual(H("foo"), "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
19861987
self.assertEqual(KD("foo", "bar"), "54dcbe67d21d5eb39493d46d89ae1f412d3bd6de")
19871988

1988-
@unittest.skipIf(get_fips_mode(), "fips mode; requires hashlib.sha256")
1989+
@skip_libssl_fips_mode
19891990
def test_sha256_algorithm(self):
19901991
H, KD = self.handler.get_algorithm_impls('SHA-256')
19911992
self.assertEqual(H("foo"), "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae")

0 commit comments

Comments
 (0)