2014-11-16 17:08:42 +00:00
|
|
|
# This file is dual licensed under the terms of the Apache License, Version
|
|
|
|
|
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
|
|
|
|
|
# for complete details.
|
2014-03-08 16:32:56 +00:00
|
|
|
|
2023-03-29 02:28:17 +00:00
|
|
|
import contextlib
|
2014-03-08 16:32:56 +00:00
|
|
|
|
2013-12-25 04:23:53 +00:00
|
|
|
import pytest
|
|
|
|
|
|
2016-03-19 20:47:30 +00:00
|
|
|
from cryptography.hazmat.backends.openssl import backend as openssl_backend
|
2014-10-23 18:01:25 +00:00
|
|
|
|
2020-12-09 16:04:47 +00:00
|
|
|
from .utils import check_backend_support
|
2014-01-14 02:52:08 +00:00
|
|
|
|
|
|
|
|
|
2021-08-29 14:05:32 +00:00
|
|
|
def pytest_configure(config):
|
|
|
|
|
if config.getoption("--enable-fips"):
|
|
|
|
|
openssl_backend._enable_fips()
|
|
|
|
|
|
|
|
|
|
|
2016-03-19 20:47:30 +00:00
|
|
|
def pytest_report_header(config):
|
2020-07-20 18:06:29 +00:00
|
|
|
return "\n".join(
|
|
|
|
|
[
|
2023-01-19 21:36:01 +00:00
|
|
|
f"OpenSSL: {openssl_backend.openssl_version_text()}",
|
|
|
|
|
f"FIPS Enabled: {openssl_backend._fips_enabled}",
|
2020-07-20 18:06:29 +00:00
|
|
|
]
|
|
|
|
|
)
|
2016-03-19 20:47:30 +00:00
|
|
|
|
2016-03-19 20:48:18 +00:00
|
|
|
|
2018-07-16 15:18:33 +00:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
|
parser.addoption("--wycheproof-root", default=None)
|
2023-11-13 19:48:28 +00:00
|
|
|
parser.addoption("--x509-limbo-root", default=None)
|
2021-08-29 14:05:32 +00:00
|
|
|
parser.addoption("--enable-fips", default=False)
|
2018-07-16 15:18:33 +00:00
|
|
|
|
|
|
|
|
|
2020-07-20 16:10:29 +00:00
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
|
if openssl_backend._fips_enabled:
|
|
|
|
|
for marker in item.iter_markers(name="skip_fips"):
|
|
|
|
|
pytest.skip(marker.kwargs["reason"])
|
|
|
|
|
|
|
|
|
|
|
2023-06-02 02:36:12 +00:00
|
|
|
@pytest.fixture(autouse=True)
|
2017-06-04 00:38:22 +00:00
|
|
|
def backend(request):
|
2017-06-04 02:02:50 +00:00
|
|
|
check_backend_support(openssl_backend, request)
|
2021-09-26 04:07:24 +00:00
|
|
|
|
|
|
|
|
# Ensure the error stack is clear before the test
|
2023-03-20 01:41:48 +00:00
|
|
|
errors = openssl_backend._consume_errors()
|
2021-09-26 04:07:24 +00:00
|
|
|
assert not errors
|
|
|
|
|
yield openssl_backend
|
|
|
|
|
# Ensure the error stack is clear after the test
|
2023-03-20 01:41:48 +00:00
|
|
|
errors = openssl_backend._consume_errors()
|
2021-09-26 04:07:24 +00:00
|
|
|
assert not errors
|
2023-03-29 02:28:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def subtests():
|
|
|
|
|
# This is a miniature version of the pytest-subtests package, but
|
|
|
|
|
# optimized for lower overhead.
|
|
|
|
|
#
|
|
|
|
|
# When tests are skipped, these are not logged in the final pytest output.
|
|
|
|
|
yield SubTests()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SubTests:
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
|
def test(self):
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
except pytest.skip.Exception:
|
|
|
|
|
pass
|