|
12 | 12 |
|
13 | 13 | import pytestskipmarkers.utils.platform |
14 | 14 |
|
| 15 | +try: |
| 16 | + import cryptography.hazmat.backends.openssl.backend as backend |
| 17 | + |
| 18 | + cryptography_import_error = "" |
| 19 | +except ImportError as exc: # pragma: no cover |
| 20 | + backend = None |
| 21 | + cryptography_import_error = f"Failed to import cryptography: {exc}" |
| 22 | + |
15 | 23 | log = logging.getLogger(__name__) |
16 | 24 |
|
17 | 25 |
|
@@ -206,6 +214,26 @@ def test_is_fips_enabled_sysctl(output, expected): |
206 | 214 | assert pytestskipmarkers.utils.platform.is_fips_enabled() is expected |
207 | 215 |
|
208 | 216 |
|
| 217 | +@pytest.mark.skipif(backend is None, reason=cryptography_import_error) |
| 218 | +@pytest.mark.parametrize("fips_enabled", [True, False], ids=lambda x: f"fips_enabled={x}") |
| 219 | +def test_is_fips_enabled_cryptography(fs, fips_enabled): |
| 220 | + fs.create_file("/proc/sys/crypto/fips_enabled", contents="0") |
| 221 | + with mock.patch("shutil.which", return_value=None): |
| 222 | + with mock.patch.object(backend, "_fips_enabled", fips_enabled): |
| 223 | + assert pytestskipmarkers.utils.platform.is_fips_enabled() is fips_enabled |
| 224 | + |
| 225 | + |
| 226 | +def test_is_fips_by_catch_exception(fs): |
| 227 | + fs.create_file("/proc/sys/crypto/fips_enabled", contents="0") |
| 228 | + with mock.patch("shutil.which", return_value=None): |
| 229 | + with mock.patch("hashlib.md5", return_value="a random md5 hash"): |
| 230 | + assert pytestskipmarkers.utils.platform.is_fips_enabled() is False |
| 231 | + with mock.patch( |
| 232 | + "hashlib.md5", side_effect=ValueError("[digital envelope routines] unsupported") |
| 233 | + ): |
| 234 | + assert pytestskipmarkers.utils.platform.is_fips_enabled() is True |
| 235 | + |
| 236 | + |
209 | 237 | def test_is_spawning_platform(): |
210 | 238 | with mock.patch("multiprocessing.get_start_method", return_value="spawn"): |
211 | 239 | assert pytestskipmarkers.utils.platform.is_spawning_platform() is True |
|
0 commit comments