From f93d8244eac195fcc7577ddf22887052c2b686ab Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Thu, 23 Oct 2014 12:07:20 -0700 Subject: filter backends during initial generation to elide tests entirely --- tests/conftest.py | 33 +++++++++++++++++++++------------ tests/utils.py | 2 -- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 20926024..a2650850 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,22 +25,31 @@ def pytest_generate_tests(metafunc): selected_backends = select_backends(names, _available_backends()) if "backend" in metafunc.fixturenames: - metafunc.parametrize("backend", selected_backends) + filtered_backends = [] + for backend in selected_backends: + try: + required = metafunc.function.requires_backend_interface + required_interfaces = tuple( + mark.kwargs["interface"] for mark in required + ) + if isinstance(backend, required_interfaces): + filtered_backends.append(backend) + except AttributeError: + # function does not have requires_backend_interface decorator + filtered_backends.append(backend) + + if not filtered_backends: + pytest.skip( + "No backends provided supply the interface: {0}".format( + ", ".join(iface.__name__ for iface in required_interfaces) + ) + ) + else: + metafunc.parametrize("backend", filtered_backends) @pytest.mark.trylast def pytest_runtest_setup(item): - required = item.keywords.get("requires_backend_interface") - if required is not None and "backend" in item.funcargs: - required_interfaces = tuple( - mark.kwargs["interface"] for mark in required - ) - if not isinstance(item.funcargs["backend"], required_interfaces): - pytest.skip("{0} backend does not support {1}".format( - item.funcargs["backend"], - ", ".join(iface.__name__ for iface in required_interfaces) - )) - check_backend_support(item) diff --git a/tests/utils.py b/tests/utils.py index bc5bc1ea..7b462c2d 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -40,8 +40,6 @@ def select_backends(names, backend_list): if names is None: return backend_list split_names = [x.strip() for x in names.split(',')] - # this must be duplicated and then removed to preserve the metadata - # pytest associates. Appending backends to a new list doesn't seem to work selected_backends = [] for backend in backend_list: if backend.name in split_names: -- cgit v1.2.3 From daefd3fbff9b83b1d714e93e46abcdc152873b80 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 24 Oct 2014 07:48:37 -0700 Subject: reorganize try block for test generation --- tests/conftest.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a2650850..fd618039 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -29,14 +29,15 @@ def pytest_generate_tests(metafunc): for backend in selected_backends: try: required = metafunc.function.requires_backend_interface + except AttributeError: + # function does not have requires_backend_interface decorator + filtered_backends.append(backend) + else: required_interfaces = tuple( mark.kwargs["interface"] for mark in required ) if isinstance(backend, required_interfaces): filtered_backends.append(backend) - except AttributeError: - # function does not have requires_backend_interface decorator - filtered_backends.append(backend) if not filtered_backends: pytest.skip( -- cgit v1.2.3 From 56f1a0ae53773285db53c367e697e0a015eed5f1 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Fri, 24 Oct 2014 10:30:25 -0700 Subject: add comment explaining some weirdness --- tests/conftest.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index fd618039..1cb2b30d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -40,6 +40,8 @@ def pytest_generate_tests(metafunc): filtered_backends.append(backend) if not filtered_backends: + # If you pass an empty list to parametrize Bad Things(tm) happen + # as of pytest 2.6.4 when the test also has a parametrize decorator pytest.skip( "No backends provided supply the interface: {0}".format( ", ".join(iface.__name__ for iface in required_interfaces) -- cgit v1.2.3 From 902d8cfd58de395748d71f449944faa9dbac8725 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 25 Oct 2014 12:22:10 -0700 Subject: move skip_if_empty to separate function for test coverage --- tests/conftest.py | 17 ++++++----------- tests/test_utils.py | 9 ++++++++- tests/utils.py | 9 +++++++++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 1cb2b30d..9dc37d38 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,7 +17,7 @@ import pytest from cryptography.hazmat.backends import _available_backends -from .utils import check_backend_support, select_backends +from .utils import check_backend_support, select_backends, skip_if_empty def pytest_generate_tests(metafunc): @@ -39,16 +39,11 @@ def pytest_generate_tests(metafunc): if isinstance(backend, required_interfaces): filtered_backends.append(backend) - if not filtered_backends: - # If you pass an empty list to parametrize Bad Things(tm) happen - # as of pytest 2.6.4 when the test also has a parametrize decorator - pytest.skip( - "No backends provided supply the interface: {0}".format( - ", ".join(iface.__name__ for iface in required_interfaces) - ) - ) - else: - metafunc.parametrize("backend", filtered_backends) + # If you pass an empty list to parametrize Bad Things(tm) happen + # as of pytest 2.6.4 when the test also has a parametrize decorator + skip_if_empty(filtered_backends, required_interfaces) + + metafunc.parametrize("backend", filtered_backends) @pytest.mark.trylast diff --git a/tests/test_utils.py b/tests/test_utils.py index 6c8d088b..8bb8422c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -32,7 +32,7 @@ from .utils import ( load_fips_ecdsa_key_pair_vectors, load_fips_ecdsa_signing_vectors, load_hash_vectors, load_kasvs_dh_vectors, load_nist_vectors, load_pkcs1_vectors, load_rsa_nist_vectors, load_vectors_from_file, - raises_unsupported_algorithm, select_backends + raises_unsupported_algorithm, select_backends, skip_if_empty ) @@ -82,6 +82,13 @@ def test_select_two_backends(): assert selected_backends == [b1, b2] +def test_skip_if_empty(): + with pytest.raises(pytest.skip.Exception): + skip_if_empty([], [FakeInterface]) + + skip_if_empty(["notempty"], [FakeInterface]) + + def test_check_backend_support_skip(): supported = pretend.stub( kwargs={"only_if": lambda backend: False, "skip_message": "Nope"} diff --git a/tests/utils.py b/tests/utils.py index 7b462c2d..acc6c141 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -53,6 +53,15 @@ def select_backends(names, backend_list): ) +def skip_if_empty(backend_list, required_interfaces): + if not backend_list: + pytest.skip( + "No backends provided supply the interface: {0}".format( + ", ".join(iface.__name__ for iface in required_interfaces) + ) + ) + + def check_backend_support(item): supported = item.keywords.get("supported") if supported and "backend" in item.funcargs: -- cgit v1.2.3 From bf148d2e11e3fbf95ce6213eb3722a4911a3f4ff Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sat, 25 Oct 2014 13:44:14 -0700 Subject: Expose all attributes via properties so they can be statically introspected Refs #1424 --- .../hazmat/backends/commoncrypto/hashes.py | 4 ++- cryptography/hazmat/backends/commoncrypto/hmac.py | 4 ++- cryptography/hazmat/backends/openssl/cmac.py | 2 ++ cryptography/hazmat/backends/openssl/hashes.py | 4 ++- cryptography/hazmat/backends/openssl/hmac.py | 4 ++- cryptography/hazmat/primitives/ciphers/modes.py | 29 ++++++++++++++-------- cryptography/hazmat/primitives/hashes.py | 4 ++- cryptography/hazmat/primitives/hmac.py | 4 ++- 8 files changed, 39 insertions(+), 16 deletions(-) diff --git a/cryptography/hazmat/backends/commoncrypto/hashes.py b/cryptography/hazmat/backends/commoncrypto/hashes.py index ebad7201..217f4e8c 100644 --- a/cryptography/hazmat/backends/commoncrypto/hashes.py +++ b/cryptography/hazmat/backends/commoncrypto/hashes.py @@ -21,7 +21,7 @@ from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.HashContext) class _HashContext(object): def __init__(self, backend, algorithm, ctx=None): - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend if ctx is None: @@ -39,6 +39,8 @@ class _HashContext(object): self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def copy(self): methods = self._backend._hash_mapping[self.algorithm.name] new_ctx = self._backend._ffi.new(methods.ctx) diff --git a/cryptography/hazmat/backends/commoncrypto/hmac.py b/cryptography/hazmat/backends/commoncrypto/hmac.py index c6e3f276..c2b6c379 100644 --- a/cryptography/hazmat/backends/commoncrypto/hmac.py +++ b/cryptography/hazmat/backends/commoncrypto/hmac.py @@ -22,7 +22,7 @@ from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.HashContext) class _HMACContext(object): def __init__(self, backend, key, algorithm, ctx=None): - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend if ctx is None: ctx = self._backend._ffi.new("CCHmacContext *") @@ -40,6 +40,8 @@ class _HMACContext(object): self._ctx = ctx self._key = key + algorithm = utils.read_only_property("_algorithm") + def copy(self): copied_ctx = self._backend._ffi.new("CCHmacContext *") # CommonCrypto has no APIs for copying HMACs, so we have to copy the diff --git a/cryptography/hazmat/backends/openssl/cmac.py b/cryptography/hazmat/backends/openssl/cmac.py index da7b7484..6a844cdc 100644 --- a/cryptography/hazmat/backends/openssl/cmac.py +++ b/cryptography/hazmat/backends/openssl/cmac.py @@ -50,6 +50,8 @@ class _CMACContext(object): self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def update(self, data): res = self._backend._lib.CMAC_Update(self._ctx, data, len(data)) assert res == 1 diff --git a/cryptography/hazmat/backends/openssl/hashes.py b/cryptography/hazmat/backends/openssl/hashes.py index da91eef6..591c014a 100644 --- a/cryptography/hazmat/backends/openssl/hashes.py +++ b/cryptography/hazmat/backends/openssl/hashes.py @@ -22,7 +22,7 @@ from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.HashContext) class _HashContext(object): def __init__(self, backend, algorithm, ctx=None): - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend @@ -44,6 +44,8 @@ class _HashContext(object): self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def copy(self): copied_ctx = self._backend._lib.EVP_MD_CTX_create() copied_ctx = self._backend._ffi.gc( diff --git a/cryptography/hazmat/backends/openssl/hmac.py b/cryptography/hazmat/backends/openssl/hmac.py index ca62184b..d5300ea0 100644 --- a/cryptography/hazmat/backends/openssl/hmac.py +++ b/cryptography/hazmat/backends/openssl/hmac.py @@ -23,7 +23,7 @@ from cryptography.hazmat.primitives import interfaces @utils.register_interface(interfaces.HashContext) class _HMACContext(object): def __init__(self, backend, key, algorithm, ctx=None): - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend if ctx is None: @@ -48,6 +48,8 @@ class _HMACContext(object): self._ctx = ctx self._key = key + algorithm = utils.read_only_property("_algorithm") + def copy(self): copied_ctx = self._backend._ffi.new("HMAC_CTX *") self._backend._lib.HMAC_CTX_init(copied_ctx) diff --git a/cryptography/hazmat/primitives/ciphers/modes.py b/cryptography/hazmat/primitives/ciphers/modes.py index 509b4de2..d995b876 100644 --- a/cryptography/hazmat/primitives/ciphers/modes.py +++ b/cryptography/hazmat/primitives/ciphers/modes.py @@ -17,10 +17,10 @@ from cryptography import utils from cryptography.hazmat.primitives import interfaces -def _check_iv_length(mode, algorithm): - if len(mode.initialization_vector) * 8 != algorithm.block_size: +def _check_iv_length(self, algorithm): + if len(self.initialization_vector) * 8 != algorithm.block_size: raise ValueError("Invalid IV size ({0}) for {1}.".format( - len(mode.initialization_vector), mode.name + len(self.initialization_vector), self.name )) @@ -30,8 +30,9 @@ class CBC(object): name = "CBC" def __init__(self, initialization_vector): - self.initialization_vector = initialization_vector + self._initialization_vector = initialization_vector + initialization_vector = utils.read_only_property("_initialization_vector") validate_for_algorithm = _check_iv_length @@ -49,8 +50,9 @@ class OFB(object): name = "OFB" def __init__(self, initialization_vector): - self.initialization_vector = initialization_vector + self._initialization_vector = initialization_vector + initialization_vector = utils.read_only_property("_initialization_vector") validate_for_algorithm = _check_iv_length @@ -60,8 +62,9 @@ class CFB(object): name = "CFB" def __init__(self, initialization_vector): - self.initialization_vector = initialization_vector + self._initialization_vector = initialization_vector + initialization_vector = utils.read_only_property("_initialization_vector") validate_for_algorithm = _check_iv_length @@ -71,8 +74,9 @@ class CFB8(object): name = "CFB8" def __init__(self, initialization_vector): - self.initialization_vector = initialization_vector + self._initialization_vector = initialization_vector + initialization_vector = utils.read_only_property("_initialization_vector") validate_for_algorithm = _check_iv_length @@ -82,7 +86,9 @@ class CTR(object): name = "CTR" def __init__(self, nonce): - self.nonce = nonce + self._nonce = nonce + + nonce = utils.read_only_property("_nonce") def validate_for_algorithm(self, algorithm): if len(self.nonce) * 8 != algorithm.block_size: @@ -109,8 +115,11 @@ class GCM(object): min_tag_length) ) - self.initialization_vector = initialization_vector - self.tag = tag + self._initialization_vector = initialization_vector + self._tag = tag + + tag = utils.read_only_property("_tag") + initialization_vector = utils.read_only_property("_initialization_vector") def validate_for_algorithm(self, algorithm): pass diff --git a/cryptography/hazmat/primitives/hashes.py b/cryptography/hazmat/primitives/hashes.py index 04f7620a..8c2284e3 100644 --- a/cryptography/hazmat/primitives/hashes.py +++ b/cryptography/hazmat/primitives/hashes.py @@ -32,7 +32,7 @@ class Hash(object): if not isinstance(algorithm, interfaces.HashAlgorithm): raise TypeError("Expected instance of interfaces.HashAlgorithm.") - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend @@ -41,6 +41,8 @@ class Hash(object): else: self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def update(self, data): if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") diff --git a/cryptography/hazmat/primitives/hmac.py b/cryptography/hazmat/primitives/hmac.py index b85fb2aa..22a31391 100644 --- a/cryptography/hazmat/primitives/hmac.py +++ b/cryptography/hazmat/primitives/hmac.py @@ -33,7 +33,7 @@ class HMAC(object): if not isinstance(algorithm, interfaces.HashAlgorithm): raise TypeError("Expected instance of interfaces.HashAlgorithm.") - self.algorithm = algorithm + self._algorithm = algorithm self._backend = backend self._key = key @@ -42,6 +42,8 @@ class HMAC(object): else: self._ctx = ctx + algorithm = utils.read_only_property("_algorithm") + def update(self, data): if self._ctx is None: raise AlreadyFinalized("Context was already finalized.") -- cgit v1.2.3