conftest.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import pytest
  6. from cryptography.hazmat.backends.openssl import backend as openssl_backend
  7. from .utils import (
  8. check_backend_support,
  9. load_wycheproof_tests,
  10. )
  11. def pytest_report_header(config):
  12. return "\n".join(
  13. [
  14. "OpenSSL: {}".format(openssl_backend.openssl_version_text()),
  15. "FIPS Enabled: {}".format(openssl_backend._fips_enabled),
  16. ]
  17. )
  18. def pytest_addoption(parser):
  19. parser.addoption("--wycheproof-root", default=None)
  20. def pytest_generate_tests(metafunc):
  21. if "wycheproof" in metafunc.fixturenames:
  22. wycheproof = metafunc.config.getoption("--wycheproof-root", skip=True)
  23. testcases = []
  24. marker = metafunc.definition.get_closest_marker("wycheproof_tests")
  25. for path in marker.args:
  26. testcases.extend(load_wycheproof_tests(wycheproof, path))
  27. metafunc.parametrize("wycheproof", testcases)
  28. def pytest_runtest_setup(item):
  29. if openssl_backend._fips_enabled:
  30. for marker in item.iter_markers(name="skip_fips"):
  31. pytest.skip(marker.kwargs["reason"])
  32. @pytest.fixture()
  33. def backend(request):
  34. required_interfaces = [
  35. mark.kwargs["interface"]
  36. for mark in request.node.iter_markers("requires_backend_interface")
  37. ]
  38. if not all(
  39. isinstance(openssl_backend, iface) for iface in required_interfaces
  40. ):
  41. pytest.skip(
  42. "OpenSSL doesn't implement required interfaces: {}".format(
  43. required_interfaces
  44. )
  45. )
  46. check_backend_support(openssl_backend, request)
  47. return openssl_backend