| 1 |
|
| 2 |
|
| 3 |
import os |
| 4 |
import pytest |
| 5 |
from global_flags import set_flag, S2N_PROVIDER_VERSION, S2N_FIPS_MODE |
| 6 |
from providers import S2N, JavaSSL, OpenSSL |
| 7 |
|
| 8 |
PATH_CONFIGURATION_KEY = pytest.StashKey() |
| 9 |
|
| 10 |
|
| 11 |
def available_providers(): |
| 12 |
""" |
| 13 |
1. determine available providers |
| 14 |
2. modify PATH to make the providers available |
| 15 |
|
| 16 |
Currently only supports s2nc/s2nd and the Java SSL client. |
| 17 |
""" |
| 18 |
providers = set() |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
expected_location = os.path.abspath("../../build/bin") |
| 23 |
for binary in ["s2nd", "s2nc"]: |
| 24 |
bin_path = f"{expected_location}/{binary}" |
| 25 |
if not os.path.exists(bin_path): |
| 26 |
pytest.fail(f"unable to locate {binary}") |
| 27 |
os.environ["PATH"] += os.pathsep + expected_location |
| 28 |
providers.add(S2N) |
| 29 |
|
| 30 |
if os.path.exists("./bin/SSLSocketClient.class"): |
| 31 |
providers.add(JavaSSL) |
| 32 |
|
| 33 |
if OpenSSL.get_provider() == "OpenSSL" and OpenSSL.get_version() >= "3.0": |
| 34 |
providers.add(OpenSSL) |
| 35 |
|
| 36 |
return providers |
| 37 |
|
| 38 |
|
| 39 |
def pytest_addoption(parser: pytest.Parser): |
| 40 |
parser.addoption( |
| 41 |
"--provider-version", |
| 42 |
action="store", |
| 43 |
dest="provider-version", |
| 44 |
default=None, |
| 45 |
type=str, |
| 46 |
help="Set the version of the TLS provider", |
| 47 |
) |
| 48 |
parser.addoption( |
| 49 |
"--best-effort-NOT-FOR-CI", |
| 50 |
action="store_true", |
| 51 |
default=False, |
| 52 |
help="""If enabled, run as many tests are possible |
| 53 |
for the discovered providers, and skip any providers |
| 54 |
that aren't available""", |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def pytest_configure(config: pytest.Config): |
| 59 |
""" |
| 60 |
pytest hook that adds the function to deselect tests if the parameters |
| 61 |
don't makes sense. |
| 62 |
|
| 63 |
This is executed once per pytest session on process startup. |
| 64 |
""" |
| 65 |
config.addinivalue_line( |
| 66 |
"markers", |
| 67 |
"uncollect_if(*, func): function to unselect tests from parametrization", |
| 68 |
) |
| 69 |
|
| 70 |
if config.getoption("--best-effort-NOT-FOR-CI"): |
| 71 |
config.stash[PATH_CONFIGURATION_KEY] = available_providers() |
| 72 |
|
| 73 |
provider_version = config.getoption("provider-version", None) |
| 74 |
|
| 75 |
|
| 76 |
if "fips" in provider_version and "openssl-1.0.2-fips" not in provider_version: |
| 77 |
set_flag(S2N_FIPS_MODE, True) |
| 78 |
set_flag(S2N_PROVIDER_VERSION, provider_version) |
| 79 |
|
| 80 |
|
| 81 |
def pytest_collection_modifyitems(config, items): |
| 82 |
""" |
| 83 |
pytest hook to modify the test arguments to call the uncollect function. |
| 84 |
""" |
| 85 |
removed = [] |
| 86 |
kept = [] |
| 87 |
for item in items: |
| 88 |
m = item.get_closest_marker("uncollect_if") |
| 89 |
if m: |
| 90 |
func = m.kwargs["func"] |
| 91 |
if func(**item.callspec.params): |
| 92 |
removed.append(item) |
| 93 |
continue |
| 94 |
kept.append(item) |
| 95 |
if removed: |
| 96 |
config.hook.pytest_deselected(items=removed) |
| 97 |
items[:] = kept |