Alex Gaynor | 5951f46 | 2014-11-16 09:08:42 -0800 | [diff] [blame] | 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. |
Alex Gaynor | c37feed | 2014-03-08 08:32:56 -0800 | [diff] [blame] | 4 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Paul Kehrer | 7e4bc6d | 2013-12-24 22:23:53 -0600 | [diff] [blame] | 7 | import pytest |
| 8 | |
Alex Gaynor | b8b1d72 | 2016-03-19 16:47:30 -0400 | [diff] [blame] | 9 | from cryptography.hazmat.backends.openssl import backend as openssl_backend |
Alex Gaynor | 7aab8b4 | 2014-10-23 11:01:25 -0700 | [diff] [blame] | 10 | |
Alex Gaynor | 2e85a92 | 2018-07-16 11:18:33 -0400 | [diff] [blame] | 11 | from .utils import ( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 12 | check_backend_support, |
| 13 | load_wycheproof_tests, |
Alex Gaynor | 2e85a92 | 2018-07-16 11:18:33 -0400 | [diff] [blame] | 14 | ) |
Paul Kehrer | 34c075e | 2014-01-13 21:52:08 -0500 | [diff] [blame] | 15 | |
| 16 | |
Alex Gaynor | b8b1d72 | 2016-03-19 16:47:30 -0400 | [diff] [blame] | 17 | def pytest_report_header(config): |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 18 | return "\n".join( |
| 19 | [ |
| 20 | "OpenSSL: {}".format(openssl_backend.openssl_version_text()), |
| 21 | "FIPS Enabled: {}".format(openssl_backend._fips_enabled), |
| 22 | ] |
| 23 | ) |
Alex Gaynor | b8b1d72 | 2016-03-19 16:47:30 -0400 | [diff] [blame] | 24 | |
Alex Gaynor | 5930305 | 2016-03-19 16:48:18 -0400 | [diff] [blame] | 25 | |
Alex Gaynor | 2e85a92 | 2018-07-16 11:18:33 -0400 | [diff] [blame] | 26 | def pytest_addoption(parser): |
| 27 | parser.addoption("--wycheproof-root", default=None) |
| 28 | |
| 29 | |
| 30 | def pytest_generate_tests(metafunc): |
| 31 | if "wycheproof" in metafunc.fixturenames: |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 32 | wycheproof = metafunc.config.getoption("--wycheproof-root", skip=True) |
Alex Gaynor | 2e85a92 | 2018-07-16 11:18:33 -0400 | [diff] [blame] | 33 | |
| 34 | testcases = [] |
Paul Kehrer | 0322962 | 2018-09-06 22:56:46 -0500 | [diff] [blame] | 35 | marker = metafunc.definition.get_closest_marker("wycheproof_tests") |
| 36 | for path in marker.args: |
Alex Gaynor | 2e85a92 | 2018-07-16 11:18:33 -0400 | [diff] [blame] | 37 | testcases.extend(load_wycheproof_tests(wycheproof, path)) |
| 38 | metafunc.parametrize("wycheproof", testcases) |
| 39 | |
| 40 | |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 41 | def 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 Gaynor | 133a179 | 2017-06-03 20:38:22 -0400 | [diff] [blame] | 47 | @pytest.fixture() |
| 48 | def backend(request): |
| 49 | required_interfaces = [ |
| 50 | mark.kwargs["interface"] |
Paul Kehrer | 0322962 | 2018-09-06 22:56:46 -0500 | [diff] [blame] | 51 | for mark in request.node.iter_markers("requires_backend_interface") |
Alex Gaynor | 133a179 | 2017-06-03 20:38:22 -0400 | [diff] [blame] | 52 | ] |
Alex Gaynor | e6055fb | 2017-06-03 22:02:50 -0400 | [diff] [blame] | 53 | if not all( |
Alex Gaynor | 133a179 | 2017-06-03 20:38:22 -0400 | [diff] [blame] | 54 | isinstance(openssl_backend, iface) for iface in required_interfaces |
| 55 | ): |
Alex Gaynor | e6055fb | 2017-06-03 22:02:50 -0400 | [diff] [blame] | 56 | pytest.skip( |
Lucia Li | c6ba99d | 2021-11-08 22:06:11 +0800 | [diff] [blame^] | 57 | "OpenSSL doesn't implement required interfaces: {}".format( |
Alex Gaynor | e6055fb | 2017-06-03 22:02:50 -0400 | [diff] [blame] | 58 | required_interfaces |
| 59 | ) |
Alex Gaynor | 133a179 | 2017-06-03 20:38:22 -0400 | [diff] [blame] | 60 | ) |
Paul Kehrer | 7e4bc6d | 2013-12-24 22:23:53 -0600 | [diff] [blame] | 61 | |
Alex Gaynor | e6055fb | 2017-06-03 22:02:50 -0400 | [diff] [blame] | 62 | check_backend_support(openssl_backend, request) |
| 63 | return openssl_backend |