Skip to content

Commit 6e2aa85

Browse files
committed
Change to spawn _test_hashlib_fips from test_hashlib with an env.
1 parent 0dbef0d commit 6e2aa85

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

Lib/test/test_hashlib_fips.py renamed to Lib/test/_test_hashlib_fips.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,17 @@
44
# Licensed to PSF under a Contributor Agreement.
55
#
66

7+
"""Primarily executed by test_hashlib.py. It can run stand alone by humans."""
8+
79
import os
8-
import sys
910
import unittest
1011

1112
OPENSSL_CONF_BACKUP = os.environ.get("OPENSSL_CONF")
1213

1314

1415
class HashLibFIPSTestCase(unittest.TestCase):
15-
_executions = 0 # prevent re-running on in refleak hunting mode, etc.
16-
1716
@classmethod
1817
def setUpClass(cls):
19-
if cls._executions > 0:
20-
raise unittest.SkipTest("Cannot run this test within the same Python process.")
21-
if sys.modules.get("_hashlib") or sys.modules.get("_ssl"):
22-
raise AssertionError("_hashlib or _ssl already imported, too late to change OPENSSL_CONF.")
2318
# This openssl.cnf mocks FIPS mode without any digest
2419
# loaded. It means all digests must raise ValueError when
2520
# usedforsecurity=True via either openssl or builtin
@@ -47,7 +42,6 @@ def tearDownClass(cls):
4742
os.environ["OPENSSL_CONF"] = OPENSSL_CONF_BACKUP
4843
else:
4944
os.environ.pop("OPENSSL_CONF", None)
50-
cls._executions += 1
5145

5246
def test_algorithms_available(self):
5347
import hashlib

Lib/test/support/script_helper.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,14 @@ def make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
303303

304304

305305
@support.requires_subprocess()
306-
def run_test_script(script):
306+
def run_test_script(script, **kwargs):
307+
"""Run the file *script* in a child interpreter.
308+
309+
Keyword arguments are passed on to subprocess.run() within.
310+
311+
Asserts if the child exits non-zero. Prints child output after
312+
execution when run in verbose mode.
313+
"""
307314
# use -u to try to get the full output if the test hangs or crash
308315
if support.verbose:
309316
def title(text):
@@ -315,7 +322,7 @@ def title(text):
315322
# In verbose mode, the child process inherit stdout and stdout,
316323
# to see output in realtime and reduce the risk of losing output.
317324
args = [sys.executable, "-E", "-X", "faulthandler", "-u", script, "-v"]
318-
proc = subprocess.run(args)
325+
proc = subprocess.run(args, **kwargs)
319326
print(title(f"{name} completed: exit code {proc.returncode}"),
320327
flush=True)
321328
if proc.returncode:

Lib/test/test_hashlib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import unittest
1818
import warnings
1919
from test import support
20+
from test.support import os_helper, script_helper
2021
from test.support import _4G, bigmemtest
2122
from test.support.import_helper import import_fresh_module
2223
from test.support import os_helper
@@ -1196,6 +1197,18 @@ def test_file_digest(self):
11961197
with open(os_helper.TESTFN, "wb") as f:
11971198
hashlib.file_digest(f, "sha256")
11981199

1200+
def test_builtins_in_openssl_fips_mode(self):
1201+
try:
1202+
from _hashlib import get_fips_mode
1203+
except ImportError:
1204+
self.skipTest('OpenSSL _hashlib not available')
1205+
from test import _test_hashlib_fips
1206+
child_test_path = _test_hashlib_fips.__file__
1207+
env = os.environ.copy()
1208+
# A config to mock FIPS mode, see _test_hashlib_fips.py.
1209+
env["OPENSSL_CONF"] = os.path.join(os.path.dirname(__file__), "hashlibdata", "openssl.cnf")
1210+
script_helper.run_test_script(child_test_path, env=env)
1211+
11991212

12001213
if __name__ == "__main__":
12011214
unittest.main()

0 commit comments

Comments
 (0)