blob: ece7a11e716a79a4de19390d12b5c2ea385192a9 [file] [log] [blame]
Alex Gaynor5951f462014-11-16 09:08:42 -08001# 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.
Alex Gaynorc37feed2014-03-08 08:32:56 -08004
5from __future__ import absolute_import, division, print_function
6
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -06007import pytest
8
Alex Gaynorb8b1d722016-03-19 16:47:30 -04009from cryptography.hazmat.backends.openssl import backend as openssl_backend
Alex Gaynor7aab8b42014-10-23 11:01:25 -070010
Alex Gaynor2e85a922018-07-16 11:18:33 -040011from .utils import (
Lucia Lic6ba99d2021-11-08 22:06:11 +080012 check_backend_support,
13 load_wycheproof_tests,
Alex Gaynor2e85a922018-07-16 11:18:33 -040014)
Paul Kehrer34c075e2014-01-13 21:52:08 -050015
16
Alex Gaynorb8b1d722016-03-19 16:47:30 -040017def pytest_report_header(config):
Lucia Lic6ba99d2021-11-08 22:06:11 +080018 return "\n".join(
19 [
20 "OpenSSL: {}".format(openssl_backend.openssl_version_text()),
21 "FIPS Enabled: {}".format(openssl_backend._fips_enabled),
22 ]
23 )
Alex Gaynorb8b1d722016-03-19 16:47:30 -040024
Alex Gaynor59303052016-03-19 16:48:18 -040025
Alex Gaynor2e85a922018-07-16 11:18:33 -040026def pytest_addoption(parser):
27 parser.addoption("--wycheproof-root", default=None)
28
29
30def pytest_generate_tests(metafunc):
31 if "wycheproof" in metafunc.fixturenames:
Lucia Lic6ba99d2021-11-08 22:06:11 +080032 wycheproof = metafunc.config.getoption("--wycheproof-root", skip=True)
Alex Gaynor2e85a922018-07-16 11:18:33 -040033
34 testcases = []
Paul Kehrer03229622018-09-06 22:56:46 -050035 marker = metafunc.definition.get_closest_marker("wycheproof_tests")
36 for path in marker.args:
Alex Gaynor2e85a922018-07-16 11:18:33 -040037 testcases.extend(load_wycheproof_tests(wycheproof, path))
38 metafunc.parametrize("wycheproof", testcases)
39
40
Lucia Lic6ba99d2021-11-08 22:06:11 +080041def pytest_runtest_setup(item):
42 if openssl_backend._fips_enabled:
43 for marker in item.iter_markers(name="skip_fips"):
44 pytest.skip(marker.kwargs["reason"])
45
46
Alex Gaynor133a1792017-06-03 20:38:22 -040047@pytest.fixture()
48def backend(request):
49 required_interfaces = [
50 mark.kwargs["interface"]
Paul Kehrer03229622018-09-06 22:56:46 -050051 for mark in request.node.iter_markers("requires_backend_interface")
Alex Gaynor133a1792017-06-03 20:38:22 -040052 ]
Alex Gaynore6055fb2017-06-03 22:02:50 -040053 if not all(
Alex Gaynor133a1792017-06-03 20:38:22 -040054 isinstance(openssl_backend, iface) for iface in required_interfaces
55 ):
Alex Gaynore6055fb2017-06-03 22:02:50 -040056 pytest.skip(
Lucia Lic6ba99d2021-11-08 22:06:11 +080057 "OpenSSL doesn't implement required interfaces: {}".format(
Alex Gaynore6055fb2017-06-03 22:02:50 -040058 required_interfaces
59 )
Alex Gaynor133a1792017-06-03 20:38:22 -040060 )
Paul Kehrer7e4bc6d2013-12-24 22:23:53 -060061
Alex Gaynore6055fb2017-06-03 22:02:50 -040062 check_backend_support(openssl_backend, request)
63 return openssl_backend